Results 1 to 5 of 5

Thread: Some wild calculations to get a pourcentage

  1. #1

    Thread Starter
    Member Birth's Avatar
    Join Date
    Jan 2003
    Location
    Montreal
    Posts
    57

    Some wild calculations to get a pourcentage

    This is a file that I compressed with my own algorithm (omacmodi.cpr) and need to read back (like a zip or something). Every line have a different lengh and I need to know how many entries are in there so I can display the pourcentage on a progress bar.

    Not only I can't find a better way to do this but I find that the calculation for "FloodPercent" is a little bit suspicious...

    It works, but is there a better way?

    VB Code:
    1. Open App.Path & "\omacmodi.cpr" For Input As #1
    2.    
    3.    ' Count the number of records (or entries).
    4.    While Not EOF(1)
    5.       Input #1, A$: NB = NB + 1
    6.    Wend
    7.    
    8.    Seek #1, 1   ' Back to beginning of file
    9.    
    10.    While Not EOF(1)
    11.    
    12.       Input #1, A$: Rec = Rec + 1
    13.      
    14.       If 100 - Int(((100 / NB * (NB - Rec)))) <> LastRec Then
    15.          Frm_Modi.Pnl_Status.FloodPercent = 100 - Int(((100 / NB * (NB - Rec))))
    16.       End If
    17.  
    18.       LastRec = 100 - Int(((100 / NB * (NB - Rec))))
    19.  
    20.       ' If I don't do this, the form is not refreshed. That's another thing!
    21.       openforms = DoEvents
    22.      
    23.       If Len(A$) <> 3 Then
    24.      
    25.          MODE$ = Left$(A$, 3)
    26.      
    27.          For I = 4 To Len(A$) - 5 Step 5
    28.          
    29.             code$ = Mid$(A$, I + 1, 5)
    30.            
    31.             TB_OM.AddNew
    32.             TB_OM("Code") = code$
    33.             TB_OM("Mode") = MODE$
    34.             TB_OM.Update
    35.          
    36.          Next
    37.      
    38.       End If
    39.      
    40.    Wend
    It took me about a day to build this like 5 years ago. It's the pourcentage that was a *****!

    Can anyone make this better or is it just a piece of art like it is? (ya)

    Thank you.

    PS: There's only 5 hits for "Some wild calculations" on Google

    ~~~~~~~~~~~~~~~~~~~~
    There's always something
    ~~~~~~~~~~~~~~~~~~~~
    <Any link censored by moderator>

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Here's a few improvements:
    VB Code:
    1. 'declare your variables!
    2. Dim iFileNo As Integer
    3. Dim iFileLen As Long
    4. Dim iLengthSoFar As Long
    5. Dim A$
    6. Dim I As Integer
    7. Dim iCurrentPercent As Integer
    8. Dim iLastPercent As Integer
    9. Dim Rec
    10.  
    11.    iFileNo = FreeFile   'Use FreeFile, not a specific number!
    12.    Open App.Path & "\omacmodi.cpr" For Input As #iFileNo
    13.    
    14.    iFileLen = LOF(iFileNo)  'Get length of file
    15.    iLengthSoFar = 0         'Store amount read so far
    16.    
    17.    While Not EOF(iFileNo)
    18.    
    19.       Input #iFileNo, A$
    20.       iLengthSoFar = iLengthSoFar + Len(A$) + 1  'nb: +1 may not be right, it may be +2
    21.      
    22.       'to get percentage use:  min% + ((counter/max_count)*(max% - min%))
    23.       'assuming you want 0 to 100, that's:  0+(counter/max_count)*(100-0)
    24.       iCurrentPercent = (iLengthSoFar / iFileLen) * 100
    25.       If iLastPercent <> iCurrentPercent Then
    26.          Frm_Modi.Pnl_Status.FloodPercent = iCurrentPercent
    27.       End If
    28.       iLastPercent = iCurrentPercent
    29.  
    30.       'openforms =   'no need for an assignment, DoEvents can be run on its own
    31.       DoEvents
    32.      
    33.       If Len(A$) <> 3 Then
    34.  
    35.          MODE$ = Left$(A$, 3)
    36.      
    37.          For I = 4 To Len(A$) - 5 Step 5
    38.          
    39.             code$ = Mid$(A$, I + 1, 5)
    40.            
    41.             TB_OM.AddNew
    42.             TB_OM("Code") = code$
    43.             TB_OM("Mode") = MODE$
    44.             TB_OM.Update
    45.          
    46.          Next I
    47.      
    48.       End If
    49.      
    50.    Wend
    51.  
    52.    Close #iFileNo

  3. #3

    Thread Starter
    Member Birth's Avatar
    Join Date
    Jan 2003
    Location
    Montreal
    Posts
    57

    Thumbs up Re: Some wild calculations to get a pourcentage

    Well hello there!

    After all those years I needed that calculation again and I think that it is time
    to thank you for your help and the attention you took to answer me.

    It works very well.

    Thank you very much si_the_geek!

    ~~~~~~~~~~~~~~~~~~~~
    There's always something
    ~~~~~~~~~~~~~~~~~~~~
    <Any link censored by moderator>

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

    Re: Some wild calculations to get a pourcentage

    No problem.. I'm surprised it's still useful to you after all this time!

  5. #5
    New Member TOQ4's Avatar
    Join Date
    Feb 2007
    Location
    Walldorf, Germany
    Posts
    7

    Re: Some wild calculations to get a pourcentage

    ' For sequential files LOC returns position divided by 128
    if (LOF(fh) \ 128)<>0 then Progress = LOC(fh) / (LOF(fh) \ 128)

    Not sure if it should read / or \... you could try.

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