Results 1 to 36 of 36

Thread: Counting by numbers with 0's added.

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    write a function to pad the number. Send it the number and an integer value representing the length it should be in characters.

    VB Code:
    1. Public Function LPAD (sNumber as String, Length as Integer) as String
    2.  
    3. LPAD = String$(Length - Len(sNumber) , "0") & sNumber
    4.  
    5. End Function

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Possibly:
    VB Code:
    1. Format(yourNumber, "000000")   'Will pad out with precceding 0's

  3. #3

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I like to do things the hard way.


    I hate it when I don't think of things like that.

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    LOL

    Actually I was just testing for speed, and I think there identical.

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Wow, it looks like the FUNCTION wins hands down

    Format ~ 500 and Function ~ 100!!!

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Private Sub Form_Load()
    5.    Dim x As Long, i As Long
    6.    Dim ans1 As Integer, ans2 As String
    7.    Dim t1 As Integer, t2 As Integer
    8.    
    9.    
    10. '************* Test First Routine ***************
    11.       x = GetTickCount  'Load the Initial count, prior to test
    12.       For i = 0 To 50000
    13.          ans1 = Format("123", "000000")
    14.       Next
    15.  
    16.       t1 = GetTickCount - x   'Determine time taken
    17.  
    18.  
    19. '************* Test Second Routine ***************
    20.       x = GetTickCount  'Load the Initial count, prior to test
    21.       For i = 1 To 50000
    22.          ans2 = LPAD("123", 6)
    23.       Next
    24.  
    25.       t2 = GetTickCount - x   'Determine time taken
    26.  
    27.  
    28. '************* Display the Results ***************
    29.       Debug.Print t1 & vbTab & t2
    30.  
    31. End Sub
    32.  
    33. Private Function LPAD(sNumber As String, Length As Integer) As String
    34.    LPAD = String$(Length - Len(sNumber), "0") & sNumber
    35. End Function


    Bruce.

  6. #6

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Bruce, I'm playing around with these function. Did you know you declared ans1 and ans2 as different types?

  7. #7
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    I am probably stupid, but...

    Dim ans2 As String
    Dim x As Integer
    Dim i As Long

    x = Text1

    For i = Text1 To Text2
    ans2 = LPAD(x, Text3)
    x = x + 1
    Next

    MsgBox "Finsihed", , "End"


    Why isn't this working ?

    text 1 = starting number
    text 2 = ending number
    text 3 = number of 0's to pad with
    ~ Animelion

  8. #8
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Yep, cause the Format returns an Integer, and the Function returns a String.

  9. #9
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Ya...

    I noticed the he declared them differently as well, but he probably had a reason.
    ~ Animelion

  10. #10
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    LOL

    Guess I should refressh the page before I goto reply.
    ~ Animelion

  11. #11

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by Bruce Fox
    Yep, cause the Format returns an Integer, and the Function returns a String.
    OK, that makes sense to me, but I wasn't sure if you did it on purpose or not.

    My computer just crashed again - new ram. I took the ram out and now have to start over again since I lost all the code.

    Basically, I'm doing it about the same as you, but I'm trying to send the number to the LPAD function as a string, a long and a variant to see the difference.

    I was 3/4 of the way there when my puter died.

  12. #12
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Thanks Cafeenman the results will be interesting.

    Animelion,
    The answer to your last question is in this tread too!
    (Hint: our loops are for testing...)

  13. #13

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Bruce, I ran each test 3 times - not scientific or anything, but the results were pretty much the same each time.

    Column 1 is the Format result
    Column 2 is the LPAD result

    Code:
    480 421  String
    471 371  Long ' best time here using Long in LPAD
    471 591  Variant
    This is the code I used. Slightly different than yours. I wanted to see how it would do with varying length numbers. The only thing I changed for each test was the type for sNumber as an argument in LPAD. I didn't change any of the types in the Form_Load event.

    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3.  
    4. Private Sub Form_Load()
    5. Dim i As Long
    6. Dim x As Long
    7. Dim sAnswer As String
    8. Dim t1 As Long
    9. Dim t2 As Long
    10.  
    11. ' Test using Format
    12.  
    13. x = GetTickCount  'Load the Initial count, prior to test
    14. For i = 0 To 100000
    15.    sAnswer = Format(Int(Rnd * 999999) + 1, "000000")
    16. Next
    17.  
    18. t1 = GetTickCount - x   'Determine time taken
    19.  
    20.  
    21. ' Test using LPAD
    22.      
    23. x = GetTickCount  'Load the Initial count, prior to test
    24. For i = 1 To 100000
    25.    sAnswer = LPAD(Int(Rnd * 999999) + 1, 6)
    26. Next
    27.  
    28. t2 = GetTickCount - x   'Determine time taken
    29.  
    30. '  Display the Results
    31.  
    32. Debug.Print t1 & vbTab & t2
    33.  
    34. MsgBox "Finished"
    35.  
    36. End Sub
    37. Private Function LPAD(lngNumber As Long, Length As Integer) As String
    38.  
    39. LPAD = String$(Length - Len(lngNumber), "0") & sNumber
    40.  
    41. End Function
    What I didn't check was to see if the function actually returned the number in the proper format. I'm pretty sure it did, but the type may need to be coerced into a string before padding it because of that thing where VB puts a space in front of a number when it turns it into a string. Do you know what I'm talking about?

    Underscore here = space

    Dim iNum as long

    iNum = 256

    Debug.Print CStr(iNum)

    ' debug window output
    _256
    Last edited by cafeenman; May 31st, 2002 at 11:17 PM.

  14. #14

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Originally posted by Bruce Fox
    Yep, cause the Format returns an Integer, and the Function returns a String.
    I read the "Yes I knew that, CafeenMan" part of this reply, but not the rest of it.

    Format returns a variant.
    Format$ returns a string.

    Why do you say it returns an integer? You're saying it's coerced? If it returns an integer, then the 0's will be dropped off, so it must get coerced into a string?

  15. #15
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Interesting, what I find wierd is difference between our
    results!
    when I tested, it looked as if the Function (although Fixed value)
    was around 500 times faster... I'm glad then that used a random
    number sequence (to test the range).

    In any case Animelion asked for the fastest way, and the Function
    (generaly) is the way to go

    I guess, there may be some overhead associated with the FORMAT
    function too.

    Cheers,
    Bruce.

  16. #16

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I edited my post above to show the fastest LPAD function. The function will need to be modified if the number is a decimal, of course.

  17. #17
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Originally posted by cafeenman

    I read the "Yes I knew that, CafeenMan" part of this reply, but not the rest of it.

    Format returns a variant.
    Format$ returns a string.

    Why do you say it returns an integer? You're saying it's coerced? If it returns an integer, then the 0's will be dropped off, so it must get coerced into a string?

    S****, sorry I missed that too..... Hmmmm

  18. #18

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    You know, we probably shoud have reversed the order in which the functions get called to be more thorough. I think it may be possible that Format is getting penalized while background things are happening with VB during start up and the form loading. Maybe I'm wrong about that, but it would be the only way to fairly test them.

  19. #19

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    OK, I just tried two other things.

    I changed Format to Format$ with no difference at all in the time.

    Then I switched the order of the functions and LPAD with the number being a long type was still faster

    371 LPAD vs. 470 for Format$

  20. #20
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    One more Q

    how can I open a text file and clear it's contents ?
    ~ Animelion

  21. #21
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    I have retested with a few changes.
    I placed the code in a cmdButton, and made ans1 String type.
    Also Format$. (and used Int(Rnd * 999999) + 1)

    The results were that same, the Function is faster.


    Bruce.

  22. #22

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Animelion - this turned out to be the fastest of what Bruce and I tried. Call it using the examples in previous posts.

    VB Code:
    1. Private Function LPAD(lngNumber As Long, Length As Integer) As String
    2.  
    3. LPAD = String$(Length - Len(lngNumber), "0") & sNumber
    4.  
    5. End Function

  23. #23

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: One more Q

    Originally posted by Animelion
    how can I open a text file and clear it's contents ?
    Using open for output erases the contents
    VB Code:
    1. Sub WriteFile ()
    2. Dim i as Long
    3. Dim sFilename as String
    4. Dim iFilenum as Integer
    5.  
    6. sFilename = app.path & "\numbers.txt"
    7.  
    8. iFilenum = FreeFile
    9.  
    10. Open sFilename For Output As #iFilenum
    11.  
    12. For i = 1 to 50000
    13.    Print #iFilenum, LPAD(i, 6)
    14. Next i
    15.  
    16. Close #iFilenum
    17.  
    18. End Sub

  24. #24

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I just thought of something else. Using a string in a loop is bad news. This would probably increase the speed for the format function.

    This way VB can pull it out of RAM instead of reallocating memory each time through the loop. I'm going to test again like this:

    VB Code:
    1. Const NUMBER_FORMAT As String = "000000"
    2.  
    3. x = GetTickCount  'Load the Initial count, prior to test
    4. For i = 0 To 100000
    5.    sAnswer = Format(Int(Rnd * 999999) + 1, NUMBER_FORMAT)
    6. Next
    7.  
    8. t1 = GetTickCount - x   'Determine time taken
    9.  
    10.  
    11. ' Test using LPAD
    12.      
    13. x = GetTickCount  'Load the Initial count, prior to test
    14. For i = 1 To 100000
    15.    sAnswer = LPAD(Int(Rnd * 999999) + 1, 6)
    16. Next
    17.  
    18. t2 = GetTickCount - x   'Determine time taken

  25. #25

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I was wrong... no improvement at all declaring the format as a const.

  26. #26
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Check this

    Dim sfilename As String
    Dim x As String
    Dim i As Long

    sfilename = App.Path & "\NumberList.txt"
    x = Text2 - 1

    Open sfilename For Output As #1
    For i = Text2 To Text3 'number to count to
    x = x + 1
    Print #1, LPAD(x, Text1)
    Next i
    Close #1

    text 1 = number of 0's to pad with
    text 2 = number to start at
    text 3 = number to end at

    For some reson it does not print to the file, however I know it is returning the right numbers.
    ~ Animelion

  27. #27
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Thanks for all that (side tracking ).
    However, I think its good to know that not allways the easiest
    path is the best (quickest).

    Cheers,
    Bruce.

  28. #28

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I have no idea what you're doing here. You're declaring x as a string and trying to count with it. If Text2 and Text3 contain numeric values, convert them first.

    Also, make sure the For loop is being entered by putting a breakpoint (F9) on the For line and then pressing F8 to step through.

    Also, you're passing the string to LPAD instead of the Length of the string.

    Try this instead:
    VB Code:
    1. Dim sfilename As String
    2. Dim i As Long
    3. dim lngMin as Long
    4. dim lngMax as Long
    5. dim iLen as Integer
    6.  
    7. lngMin= CLng(Text2.Text)
    8. lngMax = CLng(Text3.Text)
    9. iLen = CInt(Text1.Text)
    10.  
    11. sfilename = App.Path & "\NumberList.txt"
    12.      
    13. Open sfilename For Output As #1
    14.     For i = lngMin To lngMax ' number to count to
    15.         Print #1, LPAD(i, iLen)
    16.     Next i
    17. Close #1

  29. #29
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    My resons why...

    Well.... In your version LPAD(i, iLen) is problematic.

    You have i dim as an long

    then your throw it into the LPAD thing, but only something dimed as a a string can be thrown into the LPAD thing.

    As of now I get (ByRef argument type mismatch) and the i in LPAD(i, iLen) is highlighted.

    Can I temporarily redim i as something else ???

    Thanks for any help you can give.
    ~ Animelion

  30. #30

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    This is what you should be using for LPAD:
    VB Code:
    1. Private Function LPAD(lngNumber As Long, Length As Integer) As String
    2.  
    3. LPAD = String$(Length - Len(lngNumber), "0") & lngNumber
    4.  
    5. End Function

  31. #31

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    Hang on while I actually check this out. I may be giving you bad info here. I'll post when I'm sure it's right.

  32. #32

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    OK, the problem is what I thought it might be. The Len(lngNumber) thing isn't giving the right answer, so the numbers don't line up in the text file. I have no idea how the time of what I'm posting here compares to what we did already, but this will do what you need:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command_Click()
    4. Dim i As Long
    5. Dim lngMin As Long
    6. Dim lngMax As Long
    7. Dim iLen As Integer
    8. Dim sFilename As String
    9.  
    10. sFilename = App.Path & "\NumberList.txt"
    11.  
    12. iLen = CInt(Text1.Text)
    13. lngMin = CLng(Text2.Text)
    14. lngMax = CLng(Text3.Text)
    15.  
    16. Open "c:\temp\numbers.txt" For Output As #1
    17.     For i = lngMin To lngMax ' number to count to
    18.         Print #1, LPAD(CStr(i), iLen)
    19.     Next i
    20. Close #1
    21.  
    22. MsgBox "Finished"
    23.  
    24. End Sub
    25.  
    26. Private Function LPAD(sNumber As String, Length As Integer) As String
    27.  
    28. LPAD = String$(Length - Len(sNumber), "0") & sNumber
    29.  
    30. End Function

  33. #33
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Something is still wrong.

    If I set the buffer to 10 0's I get this


    higher number
    0000001638
    0000001639

    lower numbers
    0000000
    0000001


    ??? - why would it just change like that

    Is it calculating how many 0's to add based on the final number reached instead of the current number it is at ?
    ~ Animelion

  34. #34

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    it works now. Read my previous post.

  35. #35
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Just checking....

    I read the post, so your saying it doesn't work ?

    I don't get this
    "I have no idea how the time of what I'm posting here compares "
    ~ Animelion

  36. #36

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    I'm saying the last code I posted does what you want it to do.

    I do not know how the changes I made in that code compare in speed to the code we were testing. The code I last posted is slightly different than what we were testing for speed. But it does work. I put it in a project and it printed to the file with the numbers lined up as you want them to.

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