Results 1 to 9 of 9

Thread: Text values analysis

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    Lahore
    Posts
    214

    Arrow Text values analysis

    Hi everyone , have a nice day

    Take a look at this

    2007-01-06-0000,20,20,0,0,4,4,16,16,0,0,0,0,0,0,0,0
    2007-01-06-0001,28,28,0,0,2,2,25,25,0,0,0,0,1,1,0,0
    2007-01-06-0002,18,18,0,0,1,1,15,15,0,0,0,0,2,2,0,0
    2007-01-06-0003,20,20,0,0,3,3,17,17,0,0,0,0,0,0,0,0
    2007-01-06-0004,16,16,1,1,1,1,13,13,0,0,0,0,1,1,0,0
    2007-01-06-0005,18,18,0,0,2,2,16,16,0,0,0,0,0,0,0,0
    2007-01-06-0006,17,17,0,0,1,1,16,16,0,0,0,0,0,0,0,0
    2007-01-06-0007,31,31,0,0,3,3,27,27,0,0,0,0,1,1,0,0
    2007-01-06-0008,23,23,1,1,2,2,20,20,0,0,0,0,0,0,0,0
    2007-01-06-0009,19,19,1,1,1,1,17,17,0,0,0,0,0,0,0,0
    2007-01-06-0010,22,22,0,0,2,2,19,19,0,0,0,0,1,1,0,0
    2007-01-06-0011,20,20,1,1,2,2,16,16,0,0,0,0,1,1,0,0
    2007-01-06-0012,16,16,0,0,2,2,12,12,0,0,0,0,2,2,0,0

    ......................

    2007-01-06-2352,20,20,0,0,1,1,19,19,0,0,0,0,0,0,0,0
    2007-01-06-2353,25,25,1,1,3,3,19,19,0,0,0,0,2,2,0,0
    2007-01-06-2354,15,15,0,0,1,1,13,13,0,0,0,0,1,1,0,0
    2007-01-06-2355,17,17,1,1,1,1,14,14,0,0,0,0,1,1,0,0
    2007-01-06-2356,21,21,0,0,2,2,19,19,0,0,0,0,0,0,0,0
    2007-01-06-2357,19,19,0,0,3,3,15,15,0,0,0,0,1,1,0,0
    2007-01-06-2358,18,18,0,0,4,4,12,12,0,0,0,0,2,2,0,0
    2007-01-06-2359,28,28,0,0,2,2,25,25,0,0,0,0,1,1,0,0


    these are contents of my text file .. start and end of it .. as you can see it has date in begining followed by time ( just before first comma ) and then few fields (values as numbers ) separated by comma .Now what i want to do is that since .. this file is for specific day and fields are taken after every minute (as you can see the time ) .. i want accumulated values of each hour for one specfic day ( jus like you sumup sixty values for every hour to give one value ) ... like .. VB analyzes contents of each line and based on time it adds corresponding comma separated values ( 0000 to 0059 ) for first hour ... and then for second hour ( 0100 to 0159 ) and so on ... i hope i m making myself clear enuff .. if not lemme know

    Regards and thanks in advance

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Text values analysis

    You may want to think about using the Split Function
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Text values analysis

    You can use the Split() function, using comma as a separator, to separate each line into date/time and each of the 16 values. Add each value in an array of 16X24, the first value into the first of the 16 elements for that hour, etc.

    Once you have some code, if you have specific problems, post the code that's giving you the problem.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    Lahore
    Posts
    214

    Re: Text values analysis

    OOOOOOOOOOkk . so here it is .. what i have done so far and it is kinda working aswell

    assume that i have opened my text file and saved its content in string "txt"
    after that i did something like this

    VB Code:
    1. lines = Split(txt, vbNewLine)
    2. Open App.Path & "\Air1ainap.txt" For Output As #FB
    3. Debug.Print UBound(lines)
    4. For z = 1 To 1440
    5. 'Debug.Print z
    6. For i = z To z + 59
    7.  
    8.  
    9. strvalues = Split(lines(i), ",")
    10.  var1 = var1 + strvalues(2)
    11.  var2 = var2 + strvalues(8)
    12.  
    13. Next i
    14. Debug.Print var1; var2
    15. Print #FB, var1, var2, vbNewLine;
    16.  
    17. var1 = 0
    18. var2 = 0
    19. z = z + 59
    20. Next z
    21.           Close #FB
    all variables declared before this code ... this code is actually summing up data on hourly basis and putting required data in separate file !!!!!!
    now my constraint is sometimes .. input file doesnt have correct values .. like it can miss storing any one random minute data and making values to be summed up 59 from 60 .. and in such case .. my program crashes
    example of this scenario is as ..

    2007-01-06-0000,20,20,0,0,4,4,16,16,0,0,0,0,0,0,0,0
    2007-01-06-0001,28,28,0,0,2,2,25,25,0,0,0,0,1,1,0,0
    2007-01-06-0002,18,18,0,0,1,1,15,15,0,0,0,0,2,2,0,0
    2007-01-06-0003,20,20,0,0,3,3,17,17,0,0,0,0,0,0,0,0
    2007-01-06-0004,16,16,1,1,1,1,13,13,0,0,0,0,1,1,0,0
    2007-01-06-0005,18,18,0,0,2,2,16,16,0,0,0,0,0,0,0,0
    2007-01-06-0006,17,17,0,0,1,1,16,16,0,0,0,0,0,0,0,0
    2007-01-06-0008,23,23,1,1,2,2,20,20,0,0,0,0,0,0,0,0
    2007-01-06-0009,19,19,1,1,1,1,17,17,0,0,0,0,0,0,0,0
    2007-01-06-0010,22,22,0,0,2,2,19,19,0,0,0,0,1,1,0,0
    2007-01-06-0011,20,20,1,1,2,2,16,16,0,0,0,0,1,1,0,0
    2007-01-06-0012,16,16,0,0,2,2,12,12,0,0,0,0,2,2,0,0

    ......................

    2007-01-06-2352,20,20,0,0,1,1,19,19,0,0,0,0,0,0,0,0
    2007-01-06-2353,25,25,1,1,3,3,19,19,0,0,0,0,2,2,0,0
    2007-01-06-2354,15,15,0,0,1,1,13,13,0,0,0,0,1,1,0,0
    2007-01-06-2355,17,17,1,1,1,1,14,14,0,0,0,0,1,1,0,0
    2007-01-06-2356,21,21,0,0,2,2,19,19,0,0,0,0,0,0,0,0
    2007-01-06-2357,19,19,0,0,3,3,15,15,0,0,0,0,1,1,0,0
    2007-01-06-2358,18,18,0,0,4,4,12,12,0,0,0,0,2,2,0,0
    2007-01-06-2359,28,28,0,0,2,2,25,25,0,0,0,0,1,1,0,0


    there is no data of "2007-01-06-0007" ...
    which in ideal case should be there to make total values of 24 hours as 1440 well if data is missing .. i can afford to lose that accuracy like assuming that data as "0" or taking previous minute value .. but definitely i can not afford my program crashing just for this simple server mistake ..
    any ideas about how to overcome this

    Regards and thanks

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Text values analysis

    First of all "my program crashes" means nothing.. if you are getting an error, you should always tell us what the error message is, and where it occured.


    If you mean that "lines" contains fewer than 1440 items (and you are therefore getting a "Subscript out of range" error), simply use UBound(lines) instead of 1440 on the For loop.

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Text values analysis

    If the file is small then load textstream to a string using Input(LOF(filenumber),#filenumber. You can then use InStr() and InStrRev() to get the positions of the date (date will have to be formatted accordingly for InStr(), InStrRev()) and copy that to a string (eg. strDay As String) using Mid$(). Mid$() length is InStrRev() - InStr(). Then transfer result of Split(strDay, vbCrLf) to an array (eg. strDayLines() As String). Since time always starts with 0000 and increments, then the array index corresponds to the time, as follows:

    strDayLines(0) = "2007-01-06-0000,20,20,0,0,4,4,16,16,0,0,0,0,0,0,0,0"
    strDayLines(1) = "2007-01-06-0001,28,28,0,0,2,2,25,25,0,0,0,0,1,1,0,0"
    'etc etc
    strDayLines(2352) = "2007-01-06-2352,20,20,0,0,1,1,19,19,0,0,0,0,0,0,0,0"
    strDayLines(2353) = "2007-01-06-2353,25,25,1,1,3,3,19,19,0,0,0,0,2,2,0,0"

    So if you want time aggregates for the first hour, for the given day. You iterate from array index 0 to 59... for 3rd hour you iterate from 120 to 179... for nth hour you iterate from (n-1)*60 to (n-1)*60+59.

    Before proceeding to the aggregates, I suggest you update your array first. Get rid of the date info (since you already limited it to a day during transfer to strDay), and the time info (since it already corresponds with the array index). Also replace the commas with "+" using Replace() function. You should end up with:

    strDayLines(0) = "20+20+0+0+4+4+16+16+0+0+0+0+0+0+0+0"
    strDayLines(1) = "28+28+0+0+2+2+25+25+0+0+0+0+1+1+0+0"
    'etc etc
    strDayLines(2352) = "20+20+0+0+1+1+19+19+0+0+0+0+0+0+0+0"
    strDayLines(2353) = "25+25+1+1+3+3+19+19+0+0+0+0+2+2+0+0"

    Up to you if you will use the same array to store the sum per array element, or if you will use another array. Either way, you can now use the script control (using its Eval() method) to get the sum per array element.

    VB Code:
    1. Dim oScr As ScriptControl  'requires reference
    2. Dim lHour As Long  'you will pass a value to this
    3. Dim lIndex As Long
    4. Dim lSubTotal As Long
    5.  
    6. If (lHour -1) * 60 +59 <= UBound(strDayLines) Then
    7.    Set oScr = New SCriptControl
    8.    oScr.Language = "vbscript"
    9.    For lIndex = (lHour-1)*60 To (lHour-1)*60 +59
    10.       lSubTotal = lSubTotal + oScr.Eval(strDayLines(lIndex))
    11.    Next
    12. End If
    13. 'return lSubTotal
    Last edited by leinad31; Jan 15th, 2007 at 04:30 AM.

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Text values analysis

    Hmmm...missed the part about missing minute data. Rather than relying on array indices in my previous post, you can just further limit strDay using InStr() and InStrRev() but this time searching for date-time combination instead of just date. strDay will then contain info for the date and time specified.... you then use Split() on strDay and go on as before... iteration will become:

    For lIndex = 0 To UBound(strDayLines)

    EDIT: BTW, for every array element if there's a trailing comma (which will become a trailing +) either append a zero or truncate the trailing comma. Otherwise Eval() will error on the invalid expression.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Location
    Lahore
    Posts
    214

    Re: Text values analysis

    Thanks a lot
    its almost resolved just one last query
    i m doing this
    VB Code:
    1. for i = 1 to ubound(x)
    2.  
    3.  
    4. next i
    5.  Exit Sub
    6.  
    7. errorhandler:
    8.    Open App.Path & "\Errors.txt" For Output As FB
    9.         Debug.Print "An Error has occured at Block " & z / 60&; vbNewLine
    10.        
    11. Resume Next
    12. 'Close FB
    13. End Sub

    this is code of my error handler , and if my program gets more then one error , this Errors.txt only shows me the last error , is there way i can put whole error log in this file ?

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Text values analysis

    Erm.. your error handler code is not valid!

    You do not set up FB - you should have a line (just before the Open line) like: FB = FreeFile
    ..you do not close the file, which should be (before the Resume Next line) like: Close FB
    ..and you also do not write anything to the file!

    As to how to keep data that is already in the file, simply change the word Output (on the Open line) to Append.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width