This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Help with splitup a line!

Help for those learning Tcl or writing their own scripts.
Post Reply
K
KuBuntU
Voice
Posts: 1
Joined: Mon Jun 02, 2008 4:22 am

Help with splitup a line!

Post by KuBuntU »

I have this in a variable $line

Code: Select all

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.
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Re: Help with splitup a line!

Post by speechles »

KuBuntU wrote:I have this in a variable $line

Code: Select all

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: Select all

regexp -- {(.+?)\sUSER\:\s(.+?)\sIRC\:\s(.+?)} $line -> ircidentnow username savedircident
-- or like this --

Code: Select all

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.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Using [scan] it can be done like this:

Code: Select all

scan $line "%s User: %s IRC: %s" ircidentnow username savedircident
Post Reply