| View previous topic :: View next topic |
| Author |
Message |
KuBuntU Voice
Joined: 02 Jun 2008 Posts: 1
|
Posted: Mon Jun 02, 2008 4:29 am Post subject: Help with splitup a line! |
|
|
I have this in a variable $line
| Code: |
KuBuntU!~KuBuntU@xxxxxx.eu User: KuBuntU IRC: KuBuntU!~KuBuntU@xxxxxx.eu
|
and i wanted this splited up as variable.
$ircidentnow $username $savedircident
the User: and IRC: i not want in any of variables. |
|
| Back to top |
|
 |
speechles Revered One

Joined: 26 Aug 2006 Posts: 1398 Location: emerald triangle, california (coastal redwoods)
|
Posted: Mon Jun 02, 2008 6:20 am Post subject: Re: Help with splitup a line! |
|
|
| KuBuntU wrote: | I have this in a variable $line
| Code: |
KuBuntU!~KuBuntU@xxxxxx.eu User: KuBuntU IRC: KuBuntU!~KuBuntU@xxxxxx.eu
|
and i wanted this splited up as variable.
$ircidentnow $username $savedircident
the User: and IRC: i not want in any of variables. |
| Code: | | regexp -- {(.+?)\sUSER\:\s(.+?)\sIRC\:\s(.+?)} $line -> ircidentnow username savedircident |
-- or like this --
| Code: | regsub -all {(?:USER\:\s|IRC\:\s|\s\s)} $line "" line
set ircidentnow [lindex [split $line] 0]
set username [lindex [split $line] 1]
set savedircident [lindex [split $line] 2] |
Could've used 'scan' to do this as well, maybe. The top is easiest to accomplish, but if it cannot match this will not associate to any variables (it CAN fail). Eventually tcl errors or work arounds involving variables with empty contents/info exists checks will be required since at times this regexp pattern might not fit and run into code needing these variables.
The bottom way is better (it CANNOT fail). It removes user:, irc:, and double spacing from $line. Then when it splits $line to form the list it aligns perfectly to your variable assignments. _________________ speechles' eggdrop tcl archive |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Mon Jun 02, 2008 3:32 pm Post subject: |
|
|
Using [scan] it can be done like this:
| Code: | | scan $line "%s User: %s IRC: %s" ircidentnow username savedircident |
_________________ Follow me on GitHub
- Opposing
Public Tcl scripts |
|
| Back to top |
|
 |
|