$split.text()

By Lindrian on Dec 21, 2007

This is a rather simplistic snippet I made for a friend.
It simply splits your text up.
$split.text(hiiiii,32) would echo "h i"
The format is $split.text(string,C,N)

Examples:
//noop $split.text(test,32,100)
//noop $split.text(test,44,1)

The last number ($3) will specify where to split the lines.

//noop $split.text(hello whats up,44,10) would echo the following:
h,e,l,o,w,a
t,s,u,p

The "N" parameter is optional.
I don't know if there's any easier way of doing it, but this works just fine :)

  1. Now supports the propery ".dup" which will show the duplicates.
  2. Fixed a error (max split number 400)
  3. It now returns the reults if there was no split in the text, so
    it can be used as an identifier, if it splits it will echo the
    information instead.

Enjoy!

alias split.text {
  if ($isid) && ($2) {
    var %x = $remove($1,$chr(32)), %t = $2, %y = $len(%x), %i = 1, %z = 1, %h, %j = $iif($3,$iif($3 > 400,400,$3),50)
    while (%i <= %y) {
      var %b = $mid(%x,%i,1)
      if ($prop == dup) {
        var %r $+ %z %r [ $+ [ %z ] ] %b
      }
      else {
        var %r $+ %z $addtok(%r [ $+ [ %z ] ],%b,%t)
      }
      if ($numtok(%r [ $+ [ %z ] ],$iif($prop == dup,32,%t)) >= %j) inc %z
      inc %i
    }
    var %i = 1
    while (%r [ $+ [ %i ] ]) {
      var %x = %r [ $+ [ %i ] ]
      $iif(%z == 1,return,echo -a) $iif($prop == dup,$replacecs(%x,$chr(32),$chr(%t)),%x)
      inc %i
    }
  }
  else {
    $iif($isid,return,echo -ae) * $!split.text(): insufficient parameters
  } 
  return
  :error
  echo -ae * $!split.text(): insufficient parameters - $error
  reseterror
}

Comments

Sign in to comment.
Lindrian   -  Dec 23, 2007

No problemo :)

 Respond  
TropNul   -  Dec 23, 2007

Thanks for the update. :)

 Respond  
Lindrian   -  Dec 22, 2007

Fixed a few \"bugs\", see author notes.

(This was meant to be something quick, yet it ended up so \"big\" hehe)

 Respond  
opticlens   -  Dec 22, 2007

Hehe, no problem. I can be a bit vague in my comments anyway.

 Respond  
Lindrian   -  Dec 22, 2007

Oh thats right. I didnt look at it that way. Thanks.

 Respond  
opticlens   -  Dec 22, 2007
else {
  $iif($isid,return,echo -ae) %k
}

Replace with:

var %k = * $!split.text(): 
else { 
  $iif($isid,return,echo -ae) %k
}

And remove %k from *var %x = $remove($1,$chr(32)), %t = $2, %y = $len(%x), %i = 1, %z = 1, %h, %j = $iif($3,$3,50), %k = $!split.text(): **

Whilst you won\'t receive error messages in echo -ae %k $error and $iif($isid,return,echo -ae) %k

 Respond  
Lindrian   -  Dec 22, 2007

You\'re most likely talking about

  else {
    $iif($isid,return,echo -ae) %k
  } 

Atleast thats what im talking about.

And use //noop for exampple.

 Respond  
opticlens   -  Dec 22, 2007

Not exactly. You would receive an error message when using /echo because there would be no message to echo. Just move %k down into your else event.

 Respond  
Lindrian   -  Dec 22, 2007

Else event will trigger if \"if ($isid) && ($2) {\" is false...

My friend needed this to split text basically?

 Respond  
opticlens   -  Dec 22, 2007

I\'d like to know what purpose your friend had for this. Just curious to know really.

Anyway, as stated above by TropNul, you will receive no error message in your else event because there would be no %k.

Otherwise it seems to be thorough and coded well. Gj.

 Respond  
Lindrian   -  Dec 22, 2007

TropNul: Made the duplicate thingy optional, see author notes.

 Respond  
Lindrian   -  Dec 22, 2007

Well, in this matter regex doesn\'t really change much. It\'s basically the same thing, and if you want to split, you\'d still have to loop which would make it even slower.

So your code will most likely be ~5 lines shorter, but slower.

Yes, its an alternative, but not the best one :P

 Respond  
TropNul   -  Dec 22, 2007

My coding was not in any way done to compete with yours. It\'s just that I\'ve some kinda \'Perlish\' philosophy which is \"There is more than one way to do it\". As for the speed concerns, ok, i\'m beated. But that\'s not important really (for me personnally ;)) when it\'s only that amount of milliseconds on 10000 passes. And if the issues I\'ve pointed out were wanted, then ok. :o)

Cordialement

 Respond  
Lindrian   -  Dec 22, 2007

I fixed the \"error\" where it split the lines incorrectly.

The not including doubble chars was something I wanted, so thats why :P.

(And I know about that $regsubex thingy :P)
But mine is faster:
Regsubex (Yours): 1735
Regular (Mine): 937

Tested with:

//var %x = 10000, %y = $ticks | while (%x) { noop $_split(hi) | dec %x } | echo -a Regsubex: $calc($ticks - %y)

(Change the alias to $split.text to see mine)

So there you go :P

 Respond  
TropNul   -  Dec 22, 2007

There you go.

Usage: $_split(,[C])

alias _split {
  var %label = $color(info) * $!_split(<string>,[C]):
  if $isid {
    if $1 {
      var %sep = $chr($iif($2,$v1,32))
      return $left($regsubex($1,/(.)/g,\\\\1%sep),-1)
    }
    else { echo %label insufficient parameters }
  }
  else { echo %label use only as an identifier! }
}

I didn\'t included N because it\'s only \"Another way to do it\". :)

 Respond  
TropNul   -  Dec 22, 2007

It\'s a good idea but imho, there are some things that it does which should not.

1) removal of double chars (most probably because of the $addtok\'s natural behaviour)
2) even though the chars in double are not close to each other, one of them, the second one, is removed also (once more, $addtok\'s fault).
3) //echo $split.text(mirc,44,5) echoes
m,i,r
c
which imo in not what it should do because the text in input does not even contain 5 chars. When spliting at N, it should not count the Cs also.

Furthermore, the error message will never be displayed, for the simple reason that you set %k inside a condition that is out of context when an error, in this case, is to be detected.

What I would like it to do.

1) with $split.text(text,44) it should echo \"t,e,x,t\" and not \"t,e,x\"
2) with $split.text(text,44,5) it should echo \"t,e,x,t\" also and not \"t,e,x\"
\"t\"
for the same reason I gave above.

Alright for the \'review\'.

[ $mid is your friend :p ]
[ I\'ll try to scratch something like this with regexps (my passion lol) ]

Cordialement

 Respond  
Lindrian   -  Dec 22, 2007

No comments? Blargh.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.