How can I parse var, plugging each value into a, b, and c, and end up with:
% puts "$a $b $c"
1 5 0
I can't readily see how to do this. I could probably figure it out, but I
doubt with my level of tcl experience it would be the most elegant solution.
I guess I'm looking for a command that will return field N with ',' being
the field delimiter. In Bourne shell this would be something like:
a=`echo $var | cut -f1 -d,`
b=`echo $var | cut -f2 -d,`
c=`echo $var | cut -f3 -d,`
P.S. This is tcl 8.3.3 .
--
Griff Miller II
Manager of Information Technology "Beware of those who seek to win an
Positron Corporation argument at the expense of the
griff....@positron.com language." -Paul Johnson
Take a look at the split command. You can use it to split a string on a
given set of characters. In your case you would split on the comma.
However, there are pitfalls if the data isn't exactly as you say. For
example, if each item is separated by a comma and a space split won't work
quite the way you expect it. For more fancy splitting that can handle such
cases, see http://mini.net/cgi-bin/wikit/460.html and search for "advanced
split"
You can start with the csv code at:
http://mini.net/cgi-bin/wikit/721.html
or just split if you aren't doing real csv formatting.
--
Jeff Hobbs The Tcl Guy
Senior Developer http://www.ActiveState.com/
Tcl Support and Productivity Solutions
foreach {a b c} [split $var ,] { puts "$a $b $c" }
-dcr
I would do:
set lVar [split $var ,]
Then you have got a list with three elemts. You can address them by:
puts "[lindex $lVar 0] [lindex $lVar 1] [lindex $lVar 2]"
Hope this helps,
Harald
Jeff, what are the plans for the next formal release of Tcllib? I seem
to recall that some variation of the above code is in the pipeline for
the next release.
--
--
"See, he's not just anyone ... he's my son." Mark Schultz
<URL: mailto:lvi...@cas.org> <URL: http://www.purl.org/NET/lvirden/>
Even if explicitly stated to the contrary, nothing in this posting
There will be a 1.0 before the Open Source Convention. It will
have a few more bits (sha1, csv, whatever else we can throw
together). I am entering a phase of high productivity...
(read: wife and child are going on vacation without me ;^).
> % set var {1,5,0}
> How can I parse var, plugging each value into a, b, and c, and end up with:
tcllib, at least its head revision in the CVS at sourceforge, contains
a CSV module.
--
Sincerely,
Andreas Kupries <a.ku...@westend.com>
Developer @ <http://www.activestate.com/>
Private <http://www.purl.org/NET/akupries/>
-------------------------------------------------------------------------------
Perfect! Thanks - I probably could have figured that out myself, but
I was looking at all the subcommands of string, assuming the answer would
be there somewhere.
> However, there are pitfalls if the data isn't exactly as you say. For
> example, if each item is separated by a comma and a space split won't work
> quite the way you expect it. For more fancy splitting that can handle such
> cases, see http://mini.net/cgi-bin/wikit/460.html and search for "advanced
> split"
That's okay - I can assume no spaces.
Thanks again, to everyone who replied! People in this group are very helpful.
--
Griff Miller II
Manager of Information Technology
Positron Corporation "I need to be the owner of all of
griff....@positron.com the files in /usr/kvm." -Anonymous User