mIRC Homepage
Posted By: Ninko Spaces in variables - 12/06/08 09:16 PM
Hi there,
Just a quick question, is it possible to store a string in a variable that contains a space at the biginning and end of the string?
I ask because from what I can see the space at either end of the string seems to disappear?


Ninko
Posted By: qwerty Re: Spaces in variables - 12/06/08 10:11 PM
You can at the beginning but not at the end:

//var %a = $+($chr(32),a,$chr(32)) | echo -ag $replace(%a,$chr(32),-)

($replace is used to replace any spaces in the variable with dashes. This way you can see exactly where %a contains spaces)

More precisely, you can't have a variable with a single trailing space that is preceded by a non-space character; oddly enough, you can have more than one trailing spaces:

//var %a = $+(a,$chr(32),$chr(32)) | echo -ag $replace(%a,$chr(32),-)

or a single space (and nothing else):

//var %a = $chr(32) | echo -ag $replace(%a,$chr(32),-)

There are ways around this limitation. One is to use padding: just append a dummy character (or N dummy characters) when you set the var, then use $left(%var,-N) to retrieve its value without the dummy chars:

//var %a = $+(test,$chr(32)) X | echo -ag $replace($left(%a,-2),$chr(32),-)

Here the string " X" (2 chars) is appended when %a is set. Then when it is used, $left() removes the last 2 chars.
Posted By: Ninko Re: Spaces in variables - 12/06/08 10:23 PM
Thanks for the reply qwerty, I think I understand. Seems abit strange to me why those limits would be in place.

Take care


Ninko
© mIRC Discussion Forums