mIRC Homepage
Posted By: Oliver341 Spaces lost when creating variable - 26/02/07 01:00 PM
Sorry if this has been asked before but I couldn't find it when looking. I found questions about multiple consecutive spaces but I'm only dealing with singular spaces here.

My code:

Code:
test {
  var %x = 1, %toSend
  while (%x <= $len($1-)) {
    %toSend = %toSend $+ $mid($1-,%x,1)
    inc %x
  }
  say %toSend
} 


when i type:

/test one two three four

it says:

onetwothreefour

Why aren't the spaces added inside the %toSend variable during the while loop? Is there another way of doing this?

Thanks.
Posted By: Asterix_UO Re: Spaces lost when creating variable - 26/02/07 01:02 PM
try to replace the chr(32) to chr(160)
grin
Posted By: Doqnach Re: Spaces lost when creating variable - 26/02/07 01:07 PM
chr(160) (ascii 160) are non-breakable spaces and will not be stripped
Posted By: Oliver341 Re: Spaces lost when creating variable - 26/02/07 02:03 PM
Thanks, I used this:

Code:
%hacked = $replace($1-,$chr(32),$chr(160))


and then used %hacked as the string to process in the rest of the script.
Posted By: jaytea Re: Spaces lost when creating variable - 26/02/07 02:11 PM
i tend to avoid replacing with $chr(160), especially when it isn't necessary. the only problem here is that single trailing spaces in variables are cut off, but there's no problem with handling any amounts of spaces anywhere else, so looping backwards solves the problem:

Code:
test {
  var %x = $len($1-), %toSend
  while (%x) {
    set -n %toSend $mid($1-,%x,1) $+ %toSend
    dec %x
  }
  echo -a say %toSend
}


you should get into the habit of using /set's -n switch when you're handling any arbitrary piece of text (in case of /test 1 + 1 or such)

another method i've seen is to append $chr(32) each iteration, so similar to your original code but that middle line becomes:

Code:
set -n %toSend $+(%toSend,$mid($1-,%x,1),$chr(32))


this relies on the fact that a single trailing space will be stripped. so whenever $mid($1-,%x,1) != $chr(32), that space will be eaten up. of course, if it does == $chr(32), both spaces are retained.. so at the end of the loop you'll have 2 spaces for every single space in %toSend. if you pass this to a command (like /say) those excess spaces are stripped and there'll be no difference
Posted By: Oliver341 Re: Spaces lost when creating variable - 26/02/07 03:09 PM
That's great jaytea, many thanks. I don't like much replacing standard spaces with non-breaking spaces either if there's another way.

I used your "append $chr(32) on every loop" method because the reverse looping method made me greatly confused (especially as the script i'm doing adds extra characters/codes into the %toSend variable as it goes on).

Incidentally, just so I understand, how does "set -n" differ from "set" in the context of how my script works?
© mIRC Discussion Forums