mIRC Home    About    Download    Register    News    Help

Print Thread
O
Oliver341
Oliver341
O
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.

A
Asterix_UO
Asterix_UO
A
try to replace the chr(32) to chr(160)
grin

Joined: Jan 2003
Posts: 1,057
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2003
Posts: 1,057
chr(160) (ascii 160) are non-breakable spaces and will not be stripped

O
Oliver341
Oliver341
O
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.

Joined: Feb 2006
Posts: 523
J
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 523
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


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
O
Oliver341
Oliver341
O
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?


Link Copied to Clipboard