mIRC Homepage
Posted By: PhantasyX While Loop Spacing Bug? - 21/07/06 12:38 AM
I did a search for "while loop" on the entire forum for the past 6 months, and I didn't see anything directly related so..

I'm not for sure if this is a bug or if it is known but while loop fails to loop when there is a space in a variable.

I will try to demonstrate. Here is a simple script to while loop:
Code:
alias testwhile {
  var %x = 1,%y = 5
  while (%x <=  %y) {
    echo -a %x
    inc %x
  }
  echo -a Finished
}


Logically the ouput will echo the numbers 1-5, but when you modify the var part to:
"var %x = 1,%y = $+($chr(32),5)" the while loop will not echo anything and will continue to echo "Finished". A way to fix this would be to $remove character 32 before it runs thru the while loop.

I also noticed if you left the var as "%y = 5" and then change the while loop too "(%x <= %y)" (Two spaces after the equal sign.) the while loop will work.

So is this an while loop parsing the variables error? Please assist in anyway. Thanks. smile

- Zach
Posted By: RusselB Re: While Loop Spacing Bug? - 21/07/06 01:35 AM
As soon as you put that space in front of the 5 and store it in the variable, you are no longer storing a number in the variable, so the loop treats it as the equivalent of 0. Since your start value of 1 is already greater than 0, it doesn't display anything except for the Finished echo (which is showing correctly).

If you want the space to show in the display use
Code:
alias testwhile {
var %x = 1, %y = 5
while %x &lt;= %y {
echo -a $+($chr(32),%x)
inc %x
}
echo -a Finished
}
  


The while loop works with two spaces after the = sign (as you observed), due to the fact that mIRC doesn't directly support multiple spaces, so as far as mIRC is concerned, there's only one space there.

For some multiple spacing issues, check spaces.dll
Posted By: PhantasyX Re: While Loop Spacing Bug? - 21/07/06 03:01 AM
Quote:
As soon as you put that space in front of the 5 and store it in the variable, you are no longer storing a number in the variable, so the loop treats it as the equivalent of 0. Since your start value of 1 is already greater than 0, it doesn't display anything except for the Finished echo (which is showing correctly).


How is this equivalent to 0? If you check your variables tab in the script editor, the variable will show "%z 5" (Two spaces in-between).

When the variable are executed in while loop, should they not be the same as "while (1 <= 5)" (two spaces between the equal-sign and the 5).

Thus being the same as "while (%x <= %y)" (double spaced). Where %y was a regular 5 without the added space?
Posted By: starbucks_mafia Re: While Loop Spacing Bug? - 21/07/06 03:15 AM
When there's a space in a variable (or any other non-numeric character) mIRC treats that variable as text and so does a text comparison for "4" and " 5". " 5" is less than "4" for a text comparison (the ASCII code for "4" is 52), so the loop fails to execute. The parser ignores literal spaces in the code but will take into account spaces evaluated from identifiers/variables. This is the correct behaviour and it'd be very frustrating if it didn't work like that.
Posted By: DaveC Re: While Loop Spacing Bug? - 21/07/06 03:49 AM
Its all been explained already, but if u need to work out how to fix this, then use $calc() as in $calc(%y) this well evaluate out the spaces from the number.
© mIRC Discussion Forums