Jump to content
Sign in to follow this  
Santron

if then variable

Recommended Posts

Santron

I need help solving this problem.

I need to add a count to a file. I have 30 spaces and it must be left justified and right space filled.

for instance:

if $a = 2 then $answer10 = "1____________________________"

if $a = 3 then $answer10 = "2____________________________"

if $a = 4 then $answer10 = "3____________________________"

if $a = 5 then $answer10 = "4____________________________"

if $a = 6 then $answer10 = "5____________________________"

if $a = 7 then $answer10 = "6____________________________"

if $a = 8 then $answer10 = "7____________________________"

if $a = 9 then $answer10 = "8____________________________"

if $a = 10 then $answer10 = "9____________________________"

_ represents a space

If $a is a 8 then $answer10 = 7 and 29 spaces

If $a is a 20 then $answer10 = 19 and 28 spaces

I am looking for a routine that will do this for all possible numbers 1 - 999999999999999999999999999999

Thanks,

Share this post


Link to post
Share on other sites
Santron

StringFormat() will do what you want.

$answer10 = StringFormat("%-30s\n", $a-1)

Here's an example.

For $i = 2 To 11
    ConsoleWrite(StringFormat("%-30s\n", $i-1))
Next
Thank you so much for the fast reply. When I add

$answer10 = StringFormat("%-30s\n", $a-1)

and I put it in a file

$filewritecheck = $answer1 & $answer2 & $answer3 & $answer10 & $answer4

FileWriteLine($file, $filewritecheck)

The 30th spot for $answer10 becomes hex 0a (Line feed)

any ideas?

Share this post


Link to post
Share on other sites
Santron

Thank you so much for the fast reply. When I add

$answer10 = StringFormat("%-30s\n", $a-1)

and I put it in a file

$filewritecheck = $answer1 & $answer2 & $answer3 & $answer10 & $answer4

FileWriteLine($file, $filewritecheck)

The 30th spot for $answer10 becomes hex 0a (Line feed)

any ideas?

I was incorrect it adds hex 0a on the 31st spot then puts $answer4 in the line

Share this post


Link to post
Share on other sites
Santron

Yeah, I had the format wrong. It was adding a Line Feed after the spaces. I think this is right.

StringFormat("%-30s", $i-1)

Absolutely perfect.

Thanks

Share this post


Link to post
Share on other sites
Santron

Absolutely perfect.

Thanks

Ok I am attempting to figure out the StringFormat command

I need to right justify and left zero fill for 10 digits

so I thought this would work

$answer1 = InputBox("Question", "What is the number", " ", "",-1, -1, 0, 0)

$lenanswer1 = StringFormat("%010d", $answer1)

This only gives me the word error

Share this post


Link to post
Share on other sites
Spiff59

There's also the old-fashioned way:

Local $spaces = "                             "
$z = StringLeft("7" & $spaces, 30)

Local $zeros = "0000000000"
$z = StringRight($zeros & "123", 10)

For some reason it's a lot faster, if that matters.

Share this post


Link to post
Share on other sites
Santron

That Stringformat is correct. There must be something else wrong.

If I enter 1, I get 0000000001.

If I enter 12, I get 0000000012.

If I enter 123, I get 0000000123.

$answer1 = InputBox("Question", "What is the number", " ", "",-1, -1, 0, 0)

$lenanswer1 = StringFormat("%010d", $answer1)

ConsoleWrite($lenanswer1 & @CRLF)
Thanks again,

I was writing $answer1 when I should have been writing $lenanswer1

Share this post


Link to post
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
Sign in to follow this  

×