Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to parse a comma-separated list of values?

568 views
Skip to first unread message

Griff Miller

unread,
May 31, 2001, 6:17:55 PM5/31/01
to
% set var {1,5,0}

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

Bryan Oakley

unread,
May 31, 2001, 6:27:21 PM5/31/01
to
"Griff Miller" <griff....@positron.com> wrote in message
news:3B16C313...@positron.com...

> % set var {1,5,0}
>
> 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 .
>

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"

Jeff Hobbs

unread,
May 31, 2001, 7:19:15 PM5/31/01
to Griff Miller
Griff Miller wrote:
>
> % set var {1,5,0}
>
> How can I parse var, plugging each value into a, b, and c, and end up with:

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

rohn5

unread,
Jun 1, 2001, 1:15:11 AM6/1/01
to
"Griff Miller" <griff....@positron.com> wrote in message
news:3B16C313...@positron.com...
> % set var {1,5,0}
>
> 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 .
>


foreach {a b c} [split $var ,] { puts "$a $b $c" }

-dcr

Harald Oehlmann

unread,
May 31, 2001, 6:35:59 PM5/31/01
to
Dear Griff,

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

lvi...@yahoo.com

unread,
Jun 1, 2001, 10:47:10 AM6/1/01
to

According to Jeff Hobbs <Je...@ActiveState.com>:
: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, 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

Jeffrey Hobbs

unread,
Jun 1, 2001, 3:09:45 PM6/1/01
to
lvi...@yahoo.com wrote:
>
> According to Jeff Hobbs <Je...@ActiveState.com>:
> :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, 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.

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 ;^).

Andreas Kupries

unread,
Jun 1, 2001, 5:23:48 PM6/1/01
to
Griff Miller <griff....@positron.com> writes:

> % 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/>
-------------------------------------------------------------------------------

Griff Miller II

unread,
Jun 2, 2001, 3:33:44 PM6/2/01
to Bryan Oakley
Bryan Oakley wrote:
>
> "Griff Miller" <griff....@positron.com> wrote in message
> news:3B16C313...@positron.com...
> > % set var {1,5,0}
> >
> > 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 .
> >
>
> 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.

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

0 new messages