Count 00, 01, 02... in vbscript

Hey

Is there a more elegant way to count ... 00, 01, 02, 03... in vbscript


i = 0
do while i <= 20

if i < 10 then
wscript.echo "0" & i
else
wscript.echo i
end if

i = i + 1
loop
LVL 1
mikeydkAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
Not a lot better, but you could do:


for i=1 to 20
   wscript.echo Right("0" & i, 2)
loop

~bp
0
 
Martin LissConnect With a Mentor Older than dirtCommented:
This is probably faster

For i = 1 to 9
    wscript.echo "0" & i
Next
For i = 10 to 20
    wscript.echo i
Next

Open in new window

0
 
Bill PrewCommented:
@Martin,

Yes, from a pure performance point of view, that would be "faster".  But it really depends on the usage.  If it really is just to generate the numbers then I might lead toward the single loop approach, since there's likely some logic inside the loop to execute, and I wouldn't want to repeat that twice.  But we'd need more info on the use case.

For what it's worth, for the 1...20 loop, I did a test of each approach, and they were both instance, taking essentially no time.  I had to do a loop of 10000 iterations around each approach to see some differences, and yes, the Right() approach took maybe .2 secs to run, compared to .1 for your dual loop approach.  Definitely faster, and if we had to do this a million times we'd clearly want the dual loop approach.

Thanks for the thoughts, good dialog!

~bp
0
 
Martin LissOlder than dirtCommented:
You might be interested in my article of timing code.
0
 
Bill PrewCommented:
Thanks, glad that was helpful.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.