mIRC Home    About    Download    Register    News    Help

Print Thread
Spaces lost when creating variable #171588 26/02/07 01:00 PM
Joined: Mar 2006
Posts: 7
O
Oliver341 Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
O
Joined: Mar 2006
Posts: 7
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.

Re: Spaces lost when creating variable [Re: Oliver341] #171589 26/02/07 01:02 PM
Joined: May 2006
Posts: 27
A
Asterix_UO Offline
Ameglian cow
Offline
Ameglian cow
A
Joined: May 2006
Posts: 27
try to replace the chr(32) to chr(160)
grin


Suchorski @ FreeNode
Re: Spaces lost when creating variable [Re: Asterix_UO] #171591 26/02/07 01:07 PM
Joined: Jan 2003
Posts: 1,063
D
Doqnach Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2003
Posts: 1,063
chr(160) (ascii 160) are non-breakable spaces and will not be stripped


If it ain't broken, don't fix it!
Re: Spaces lost when creating variable [Re: Doqnach] #171597 26/02/07 02:03 PM
Joined: Mar 2006
Posts: 7
O
Oliver341 Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
O
Joined: Mar 2006
Posts: 7
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.

Re: Spaces lost when creating variable [Re: Oliver341] #171598 26/02/07 02:11 PM
Joined: Feb 2006
Posts: 546
J
jaytea Offline
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
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
Re: Spaces lost when creating variable [Re: jaytea] #171609 26/02/07 03:09 PM
Joined: Mar 2006
Posts: 7
O
Oliver341 Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
O
Joined: Mar 2006
Posts: 7
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?