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:
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:
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.
