Split Vector

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kalyan Ram
    New Member
    • Feb 2012
    • 7

    Split Vector

    I have date and time combined in a vector and i want to split the date and time and store them in a vector again for comparision.
    The below is the format of datetime vector
    For example :-
    25/Jul/2011:00:00:32
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Why not just keep the date and time as you have it now and do any splittng inside the comparison function?

    It doesn't look like you need a second vector.
    Last edited by weaknessforcats; Feb 20 '12, 04:02 PM. Reason: typo.

    Comment

    • Kalyan Ram
      New Member
      • Feb 2012
      • 7

      #3
      Hi,
      Thanks for the reply :)
      I can do that in comparision function but even for that i need to split the vector datetime. My requirement is i need to see the time for a particular day... to make it brief date is constant but i need to see the time which changes so for this i want to split them.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        If the date is constant, aren't all the times for the same day?

        Comment

        • Kalyan Ram
          New Member
          • Feb 2012
          • 7

          #5
          yes the time is for the same day...
          If i can split it then i can count how many times per day i got web hits

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            What is the data type that the vector contains? Is it a string or is it some binary type.

            If the dates are constant (i.e. all the same) then a simple string comparison will do the job for you if you want to sort.

            However having read all the posts you appear to be trying to analyse the data. If the dates are truly all the same then the number of hits per day you just be the size of the vector, no need for any analysis.

            However if you have loaded the dates from a server log into a vector then presumably the dates are not all the same. You could write a simple function to use with std::transform to strip the time off the string and store in a new vector.

            However if there are a very large number of entries it may be better to create a set of unique dates and then use that with std::count_if to analyse the data in the original vector because it will use less memory.

            Comment

            • Kalyan Ram
              New Member
              • Feb 2012
              • 7

              #7
              Hi,
              the datatype is string.
              the access log file contains data in daily basis and am storing the web hits in each vector but i got date and time in same vector which i want to split and i have huge number of web hits

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                OK my point here is that if you solely want the number of hits per day then that is simply the number of entries in the vector if a single file is the data for a single day. You have no need to split the strings a simple vector::size call will give the answer. In fact you do not need to even load the vector, a count of the number of lines in the log file should give the answer.

                From what you have posted it is not clear why you would need to split the strings or indeed do anything to them.

                However if you where also trying to get hits per hour over the day or some simlar processing they you might. However as weaknessforcats has pointed out since all the dates are the same that data is just not required, you could have a single vector with just the times in it by processing the string before you put it into the vector.

                Look up string::find_fi rst_of and string::substr, these 2 methods should enable you to do what you want.

                Another option is rather than store a string to store the second in the day, the conversion is slightly harder but you could fit it into a 4 byte integer using about 50% less memory. Comparisons would be quicker too compared to the string comparisons you would have to do otherwise.

                Comment

                • Kalyan Ram
                  New Member
                  • Feb 2012
                  • 7

                  #9
                  To make my requirement more clear...
                  First thing is i have a log file which i do consider and in that i have web hits which contains ip address , userid , date & time , link to which the Customer logs in.Now my requirement is i need to get the web hits for a particular Customer for a configurable time period. So, for this I have split the web hit by spaces and used a switch case through which i have stored each variable(ip address,userid etc) into a vector. Now in vec1 i have ip address,vec2 i have userid and so on. But here my problem is i couldn't split a vector which has date and time. If i can split it then i can compare previous vector with the present vector if the web hit is of the same customer i can retrieve the usage for a configurable time period.I need the time period seperately as i need to count the web hits for time(example 00:01:00)

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    You would be better off placing the userid, date and time, etc in struct. Store the struct in your vector. Use another struct for the date and time an put an instance of that Date struct inside the one for the Customer's data.

                    Now everything is separated so you can get at it:

                    Code:
                    struct LogEntry
                    {
                        Customer* c;
                        Date dt;
                        etc...
                    };
                    where
                    struct Date
                    {
                        int month;
                        int day;
                        int year;
                        int hh;
                        int mi;
                        int sec;
                    };
                    Now you should be able to write a function that iterates the LogEntry instances in your vector and if it is the correct Customer, and the Date is within range, then...well you can go from here.

                    You may find that this is easier than additional vectors and frequent data parsing.

                    Comment

                    • Kalyan Ram
                      New Member
                      • Feb 2012
                      • 7

                      #11
                      Thanks for the solution weaknessforcats but i'm not looking for struct. I dont want to use struct in my coding.so, could u please provide me a solution as i asked for splitting a vector or conversion of vector to string so that i can go in the further procedure

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        Thanks for the solution weaknessforcats but i'm not looking for struct. I dont want to use struct in my coding.
                        Why on earth not?

                        You are talking about having a vector full of strings and then processing each string every time you want some information and creating a new vector. That will be costly in both processor time and memory used.

                        If rather than a string you store a structure of values that have already been processed from the string you will in general save both time and memory.

                        The pseudo code for what you want is something like this

                        Code:
                        // Pull the log file into memory
                        OPEN LOG
                        
                        FOREACH LINE IN LOG
                          READ LINE
                          PUT LINE IN VECTOR 1
                        
                        // Process data in memory
                        FOREACH ENTRY IN VECTOR
                          GET ENTRY
                          PROCESS ENTRY INTO NEW STRING
                          STORE STRING IN VECTOR 2
                        
                        FOREACH ENTRY IN VECTOR 2
                          ANALYSE DATA
                        
                        PRODUCE RESULT FROM DATA ANALYSIS
                        For each type of analysis you want to do you will have to repeat lines 8 - 17

                        What weaknessforcats is suggesting (which in my opinion is a much better solution) is

                        Code:
                        // Pull the log file into memory
                        OPEN LOG
                        
                        FOREACH LINE IN LOG
                          READ LINE
                          PROCESS LINE INTO STRUCTURE
                          PUT STRUCTURE IN VECTOR 1
                        
                        // Process data in memory
                        FOREACH ENTRY IN VECTOR 1
                          ANALYSE DATA
                        
                        PRODUCE RESULT FROM DATA ANALYSIS
                        Here for each type of analysis you want to do you will have to repeat lines 9 - 13. On the whole it will be much more efficient.

                        If you insist on processing a vector of strings into another vector of strings then take a look at the std::string reference, also look up std::transform from the STL algorithms which could be of use in this case.

                        Comment

                        • Kalyan Ram
                          New Member
                          • Feb 2012
                          • 7

                          #13
                          It is not regarding split vector.
                          Can anyone please help me in getting a variable from one class to another class.
                          To make it brief... I have a class x and in that i have a method xy which has a string variable p which is declared as a local variable. I have another class y in which i want to get the string variable p.
                          The below is my code :-
                          I have Hit.cpp file which has the code as below :-

                          string Hit::convertDat e(const string& dateString,cons t string& hit)
                          {
                          string timing = dateString.subs tr(13,20)
                          }

                          I have another code Server.cpp file which has as below :-

                          Hit h(hit,pMapIP2II P,userAgent);

                          In Server.cpp file i want to use the variable timing and it to get processed same as it is working in Hit.cpp file.

                          Need quick response please !!!!

                          Comment

                          • weaknessforcats
                            Recognized Expert Expert
                            • Mar 2007
                            • 9214

                            #14
                            Plesase start a new thread. Passing data between objects is off the subject of your split vector.

                            Comment

                            Working...