mIRC Home    About    Download    Register    News    Help

Print Thread
String Theory (er..theory of strings) #186371 21/09/07 07:19 PM
Joined: Mar 2004
Posts: 27
A
AltReality Offline OP
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

Re: String Theory (er..theory of strings) [Re: AltReality] #186375 21/09/07 08:02 PM
Joined: Jan 2006
Posts: 111
N
noMen Offline
Vogon poet
Offline
Vogon poet
N
Joined: Jan 2006
Posts: 111
Will this help?

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

Re: String Theory (er..theory of strings) [Re: noMen] #186381 21/09/07 09:46 PM
Joined: Mar 2004
Posts: 27
A
AltReality Offline OP
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.
Re: String Theory (er..theory of strings) [Re: AltReality] #186382 21/09/07 10:03 PM
Joined: Jan 2006
Posts: 111
N
noMen Offline
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.
Re: String Theory (er..theory of strings) [Re: noMen] #186397 22/09/07 12:56 AM
Joined: Aug 2006
Posts: 183
T
Thrull Offline
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
Re: String Theory (er..theory of strings) [Re: Thrull] #186442 22/09/07 08:11 PM
Joined: Jul 2006
Posts: 107
L
LonDart Offline
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
Re: String Theory (er..theory of strings) [Re: LonDart] #186444 22/09/07 08:45 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline
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?

Re: String Theory (er..theory of strings) [Re: DJ_Sol] #186447 22/09/07 09:09 PM
Joined: Jul 2006
Posts: 107
L
LonDart Offline
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
Re: String Theory (er..theory of strings) [Re: AltReality] #186474 23/09/07 06:20 AM
Joined: Feb 2005
Posts: 342
R
Rand Offline
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

Re: String Theory (er..theory of strings) [Re: LonDart] #186480 23/09/07 10:09 AM
Joined: Aug 2006
Posts: 183
T
Thrull Offline
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