Jump to content

For Next getting the 0 in front of single numbers


Recommended Posts

Hey guys,

i use this function a lot, when performing actions on remote computers.

computers are named:

xxx-Room-01

and so forth.

at the moment, whenever i create a For Next statement, i do the following:

For $x = 8 to 11
    $add0 = StringLen($x)
    If $add0 = 1 Then $x = String("0"&$x)
MsgBox(0, "hellow World", $x)
Next

is there any way to do this, and use less code ?

Link to comment
Share on other sites

Here's one way:

For $x = 8 To 11
    $add0 = StringFormat("%02d", $x)
    MsgBox(0, "hellow World", $add0)
Next

Edit: too slow, Helge beat me to it.

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

  • Moderators

Hey guys,

i use this function a lot, when performing actions on remote computers.

computers are named:

xxx-Room-01

and so forth.

at the moment, whenever i create a For Next statement, i do the following:

For $x = 8 to 11
    $add0 = StringLen($x)
    If $add0 = 1 Then $x = String("0"&$x)
MsgBox(0, "hellow World", $x)
Next

Edit:

That's kind of funny... like a machine going off with answers.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

no... we run around 200 last count.

but each is in a different lab.. an no lab is over 30.. so 2 digits is fine.

thanks for the help guys, i had worked it into one line myself just after asking, i had this:

For $x = 8 to 11
If StringLen($x) = 1 Then $x = String("0"&$x)
MsgBox(0, "hellow World", $x)
Next

so i compressed my two lines into one, bu i guess the more proffessional way to go would be the string format.

Cheers

/tAK

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...