For Programmers: Free Programming Magazines  


Home > Archive > Tcl > May 2006 > But how do I do this in sh?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author But how do I do this in sh?
Christopher Nelson

2006-05-26, 8:05 am

Tcl has a lovely idiom for parsing lists and setting variables. If I
have "a:b:c:d" and I want those four values in four variables, I can
do:

set l "a:b:c:d"
foreach { A B C D } [split $l] { }

(Or whatever, I haven't tested this. You know what I mean.) but I'm
stuck right now coding in sh. Any bilingual coders here know a neat
trick to parse and assign in sh?

Gerald W. Lester

2006-05-26, 8:05 am

Christopher Nelson wrote:
> Tcl has a lovely idiom for parsing lists and setting variables. If I
> have "a:b:c:d" and I want those four values in four variables, I can
> do:
>
> set l "a:b:c:d"
> foreach { A B C D } [split $l] { }
>
> (Or whatever, I haven't tested this. You know what I mean.) but I'm
> stuck right now coding in sh. Any bilingual coders here know a neat
> trick to parse and assign in sh?


BTW, it is [split $l :] in Tcl, otherwise you would have:
A == C
B == :
C == d
D == {}

As to how to do this in sh, have the faintest -- I'd just write that shell
script in Tcl instead!

--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
Uwe Klein

2006-05-26, 8:05 am

Christopher Nelson wrote:
> Tcl has a lovely idiom for parsing lists and setting variables. If I
> have "a:b:c:d" and I want those four values in four variables, I can
> do:
>
> set l "a:b:c:d"
> foreach { A B C D } [split $l] { }
>
> (Or whatever, I haven't tested this. You know what I mean.) but I'm
> stuck right now coding in sh. Any bilingual coders here know a neat
> trick to parse and assign in sh?
>

Well, it is not "bash"ing neat ...

val=1:2:3:4

echo X1:
echo ${val//:/ } \
| (
read A B C D X
echo A:$A B:$B C:$C D:$D
)

echo X2:
foreach=":$val"
for tok in E F G H ; do
foreach="${foreach/:/ ${tok}=}"
done
eval $foreach
echo E:$E F:$F G:$G H:$H

reading into an array is easier:
echo X3:
array=( ${val//:/ } )

echo A:${array[0]} B:${array[1]} C:${array[2]} D:${array[3]}




uwe
Schelte Bron

2006-05-26, 8:05 am

Christopher Nelson wrote:
> Tcl has a lovely idiom for parsing lists and setting variables.
> If I have "a:b:c:d" and I want those four values in four
> variables, I can do:
>
> set l "a:b:c:d"
> foreach { A B C D } [split $l] { }
>
> (Or whatever, I haven't tested this. You know what I mean.) but
> I'm
> stuck right now coding in sh. Any bilingual coders here know a
> neat trick to parse and assign in sh?


This works in bash:

l="a:b:c:d"
IFS=":"
set $l
IFS=""

Your values are now in $1, $2, $3 and $4

Schelte.
--
set Reply-To [string map {nospam schelte} $header(From)]
Donal K. Fellows

2006-05-26, 8:05 am

Christopher Nelson wrote:
> Tcl has a lovely idiom for parsing lists and setting variables. If I
> have "a:b:c:d" and I want those four values in four variables, I can
> do:
>
> set l "a:b:c:d"
> foreach { A B C D } [split $l] { }
>
> (Or whatever, I haven't tested this. You know what I mean.) but I'm
> stuck right now coding in sh. Any bilingual coders here know a neat
> trick to parse and assign in sh?


It's a yucky hack.

saveIFS=$IFS;IFS=':'
set dummy $l
IFS=$saveIFS
shift
A=$1;B=$2;C=$3;D=$4

If you were iterating over the values for real, I'd structure it
slightly differently, but the principle would be about the same.

Donal.
Christopher Nelson

2006-05-26, 8:05 am

Schelte Bron wrote:
> Christopher Nelson wrote:
>
> This works in bash:
>
> l="a:b:c:d"
> IFS=":"
> set $l
> IFS=""
>
> Your values are now in $1, $2, $3 and $4


Beautiful! It even works in BusyBox's pseudoshell. I _knew_ there was
a trick but couldn't remember it. Thanks!

Ralf Fassel

2006-05-26, 7:06 pm

* Schelte Bron <nospam@wanadoo.nl>
| This works in bash:
|
| l="a:b:c:d"
| IFS=":"
| set $l

Nitpick: better make that
set -- $l

Just in case $l starts with a dash when arbitrary input comes into
play.

R'
Bruce Hartweg

2006-05-26, 7:06 pm

Gerald W. Lester wrote:
> Christopher Nelson wrote:
>
> BTW, it is [split $l :] in Tcl, otherwise you would have:
> A == C
> B == :
> C == d
> D == {}
>

actually, default separator in split is whitespace, so yuou would get

A = "a:b:c:d"
B = ""
C = ""
D = ""

Bruce


> As to how to do this in sh, have the faintest -- I'd just write that
> shell script in Tcl instead!
>


me neither, but luckily others have chimed in

Bruce
Gerald W. Lester

2006-05-26, 10:03 pm

Bruce Hartweg wrote:
> Gerald W. Lester wrote:
> actually, default separator in split is whitespace, so yuou would get
>
> A = "a:b:c:d"
> B = ""
> C = ""
> D = ""
>


Right I was thinking he typed [split $l {}]

> Bruce
>
>
>
> me neither, but luckily others have chimed in
>
> Bruce



--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com