mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Mar 2004
Posts: 27
A
Ameglian cow
OP Offline
Ameglian cow
A
Joined: Mar 2004
Posts: 27
Hi again,

Quick question...
I have a string of text in a variable...

%text |a|b|c||||d|||e|||

I want to use $gettok() on this string to separate it out into variables...but when there is no data between the ||'s, it is skipped. In the above example, $gettok(%text,4,124) == d, not null. (I guess $gettok can't pull a null value...makes sense.)
If there is a space between the ||'s, then it will $gettok the space.

So what would be the most effective way to break this line up and insert spaces in-between the ||'s ? I'm guessing a while loop, and a replace function...but can anyone think of anything else to try?

Thanks,
AltReality

Joined: Jan 2006
Posts: 111
N
Vogon poet
Offline
Vogon poet
N
Joined: Jan 2006
Posts: 111
Will this help?

$gettok($replace(%text, |, $+(|,$chr(32))), $1, 124)

Joined: Mar 2004
Posts: 27
A
Ameglian cow
OP Offline
Ameglian cow
A
Joined: Mar 2004
Posts: 27
I actually figured out an easier way...

Code:
While ($chr(124) $+ $chr(124) isin %text) { %text = $replace(%text,$chr(124) $+ $chr(124),$chr(124) $chr(124)) }


This runs through the loop twice (once doesn't work for some reason)...

It requires using $chr(124) rather than | because it thinks you are trying to launch a new line in the code.

I'm going to be using the text string for several operations after that, so using the gettok you provided 20 times in the script would get cumbersome smile

Thanks for the reply though! smile

Last edited by AltReality; 21/09/07 09:52 PM.
Joined: Jan 2006
Posts: 111
N
Vogon poet
Offline
Vogon poet
N
Joined: Jan 2006
Posts: 111
You could also do:

Code:
set %text $replace(%text, |, $+(|,$chr(32)))
.....
$gettok(%text, $1, 124) 


The reason why your while has to run twice is because you have three ||| next to each other. After the first loop, the first two || are replaced by one |, leaving two ||. So you need to run the loop again.

Last edited by noMen; 21/09/07 10:05 PM.
Joined: Aug 2006
Posts: 183
T
Vogon poet
Offline
Vogon poet
T
Joined: Aug 2006
Posts: 183
Gotta love Regex for stuff like this:

Code:
alias TR {
  var %test = |a|b|c||||d|||e|||
  var %testReg = /\|{2,}/Sig
  noop $regsub(Blah,%test,%testReg,|, %TestOutput)
  /echo -ag %Testoutput
}


Result: |a|b|c|d|e|

Also, it won't matter how many | you have in a row in this case. As long as there are 2 or more, it'll replace them with a single |

Last edited by Thrull; 22/09/07 12:59 AM.

Yar
Joined: Jul 2006
Posts: 107
L
Vogon poet
Offline
Vogon poet
L
Joined: Jul 2006
Posts: 107
That's not what he wants to do. He wants to preserve all of the pipes, and
insert a space between any two consecutive pipes with no other data between them.
Presumably the position of his toks must be maintained.

I am still struggling with regex, but this seems to work:
Code:
alias TR {
  var -s %test = |a|b|c||||d|||e|||
  var %testReg = /\|(?=\|)/Sig
  noop $regsub(Blah,%test,%testReg,$+($chr(124),$chr(32)),%TestOutput)
  /echo -ag %Testoutput
}

Last edited by LonDart; 22/09/07 08:28 PM.

LonDart
Joined: Jan 2007
Posts: 1,156
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
When creating the string of text seperated by | why not put something in the $null spaces?

Joined: Jul 2006
Posts: 107
L
Vogon poet
Offline
Vogon poet
L
Joined: Jul 2006
Posts: 107
Beats me. He doesn't say how he's creating the variable.
It would make the most sense to alter that.


LonDart
Joined: Feb 2005
Posts: 342
R
Fjord artisan
Offline
Fjord artisan
R
Joined: Feb 2005
Posts: 342
Using regex is one of the easier ways to deal with this.

Regex (Regular Expressions) gives you a couple different ways to handle this.

The first way, is to insert spaces between the double consecutive ||s, and use $gettok() to get them.

The second way, is to use $regex() and use $regml() instead of $gettok.

The first way:
Code:
alias examp1 {
  var %text = |a|b|c||||d|||e|||
  var %text = $regsubex(%text,/(?<=\|)(?=\|)/g,$chr(32))
  var %i = 1 , %n = $numtok(%text,124)
  while (%i <= %n) {
    echo -a :: %i : $qt($gettok(%text,%i,124))
    inc %i
  }
}


The second way:
Code:
alias gettest2 {
  var %text = |a|b|c||||d|||e|||
  noop $regex(%text,/\|(.*?)(?=\|)/g)
  var %i = 1 , %n = $regml(0)
  while (%i <= %n) { 
    echo -a :: %i : $qt($regml(%i))
    inc %i
  }
}



For string manipulation, I highly recommend learning regular expressions. They may look a bit complex, and even overwhealming to those who aren't familiar with them, however, once you learn how to write* them, you'll find operations like this much more easy to deal with.


Note on "write*": They do become much easier to write as you learn, however, they will still be a pain to read at times. smile

Joined: Aug 2006
Posts: 183
T
Vogon poet
Offline
Vogon poet
T
Joined: Aug 2006
Posts: 183
Gah, sorry. Completely misread the question... Again!

I shouldn't post when I'm tired. blush


Yar

Link Copied to Clipboard