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.

comma seperated list

Old posts that have not been replied to for several years.
Locked
J
Jagg
Halfop
Posts: 53
Joined: Sat Jan 24, 2004 11:32 am

comma seperated list

Post by Jagg »

Hi,

if I have eg

"2,4,6,8,10,12,14" in a variable. How can "split" that into 9 "different variables" so that

eg $3 gives me 6

$1 gives me 2

and so on....

Thanks
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Simple, split then use an for to make the variables.
Once the game is over, the king and the pawn go back in the same box.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Code: Select all

proc tokenize {str {split " "}} {
	set i -1
	foreach v [split $str $split] {
		uplevel 1 [list set [incr i] $v]
	}
	incr i
}
usage: tokenize $yourString ","
returns: the number of variables created (the first one is "0")
Have you ever read "The Manual"?
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

user wrote:

Code: Select all

proc tokenize {str {split " "}} {
	set i -1
	foreach v [split $str $split] {
		uplevel 1 [list set [incr i] $v]
	}
	incr i
}
usage: tokenize $yourString ","
returns: the number of variables created (the first one is "0")
this seems realy complicated just to split a ',' is it faster ? does uplevel export it outside the proc ? or something cause unless its doing that i must be missing something cause the tokenize is local..

sorr just curious can't get a grip on uplevel just yet i know it is something like eval but .. thats it
XplaiN but think of me as stupid
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Also string map can do the job for you, in only line. Regsub is also an alternative, but string map is faster. :)

Replace the comma's by an empty space. Now you have created a list with elements. Then foreach element in the list make a variable. Use a counter for the variable's name and the value will be the element.
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
J
Jagg
Halfop
Posts: 53
Joined: Sat Jan 24, 2004 11:32 am

Post by Jagg »

Thanks for your answers!!!
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

http://www.google.com/search?q=clue

Post by user »

Ofloo wrote:this seems realy complicated just to split a ',' is it faster ? does uplevel export it outside the proc ? or something cause unless its doing that i must be missing something cause the tokenize is local..
It does what he requested. (creates the variable(s) in the scope the proc is invoked from)
awyeah wrote:Replace the comma's by an empty space. Now you have created a list with elements.
Wrong. You also need to escape alot of special characters. 'split' exists for a reason.
Have you ever read "The Manual"?
Locked