All pastes #70503 Raw Edit

Split

public text v1 · immutable
#70503 ·published 2006-06-24 10:38 UTC
rendered paste body
http://www.mircscripting.info/forum/viewtopic.php?t=601

/*

Explanation: this snippet splits text up by a character or string you specify, it then fills $s1..$s2..$sN just like /tokenize does $1..$2..$N (/help $1-)

Usage:
Exactly the same as /tokenize, but instead of supplying an ascii number, you specify a character or string.
The only extra thing is the -c switch, which can be used if you want to clear the token list for whatever reason.

Examples:
//split ! a!b | echo -a $!s1: $s1 ~ $!s2: $s2 ~ $!s1-: $s1- ~ $!s1-2: $s1-2 ~ $!s2-: $s2- ~ $!s0: $s0
//split helloworld 1helloworld2 | echo -a $!s1: $s1 ~ $!s2: $s2 ~ $!s1-: $s1- ~ $!s1-2: $s1-2 ~ $!s2-: $s2- ~ $!s0: $s0

Credits: Propogater @ DALNet #helpdesk - finding a bug where if you used /split when you'd already split up more tokens it would fail.

*/

alias split {
  if ($1 == -c) { var %c = 1 }
  tokenize 13 $replace($2-,$1,$cr)
  var %i = 1, %g
  .alias s0
  while ($isalias(s $+ %i)) {
    .alias s $+ %i
    %g = %i + 1
    while ($isalias($+(s,%i,-,%g))) {
      .alias $+(s,%i,-,%g)
      inc %g
    }
    .alias $+(s,%i,-)
    inc %i
  }
  if (!%c) {
    %i = 1
    while ($eval($ $+ %i,2) != $null) {
      if (%i < $0) {
        %g = %i + 1
        while ($eval($ $+ %g,2) != $null) {
          .alias $+(s,%i,-,%g) return $eval($+($,%i,-,%g),2)
          inc %g
        }
      }
      .alias $+(s,%i) return $eval($ $+ %i,2)
      .alias $+(s,%i,-) return $eval($+($,%i,-),2)
      inc %i
    }
    .alias s0 return $calc(%i - 1)
  }
}