Home > Archive > Tcl > June 2005 > foreach problem
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]
|
|
| Hans Herrmann 2005-06-05, 8:57 pm |
| Hi all,
I know its simple but ...???
set l 1,0
set b 2,0
foreach pos {l b} {
resub -all {,} $pos {.} pos
}
The result should be:
set l -> 1.0
set b -> 2.0
The problem is "pos"?! But what's correct?
Thanks
Hans
| |
| Darren New 2005-06-05, 8:57 pm |
| Hans Herrmann wrote:
> Hi all,
> I know its simple but ...???
>
> set l 1,0
> set b 2,0
>
> foreach pos {l b} {
Here, pos takes on the values of "l" and "b".
> resub -all {,} $pos {.} pos
So here, you're substituting periods for commas in "l" and "b", and
assigning the result to pos.
What you probably want is another level of look-up-the-variable, so the
regsub line should be
regsub -all {,} [set $pos] {.} [set pos]
The first time thru the loop, [set pos] returns "l" and [set $pos]
returns "1,0". SO this translates to
regsub -all , 1,0 . l
which is what you want.
Basically, when you're looping over the names of variables, you have to
put an extra indirection in there.
HTH.
--
Darren New / San Diego, CA, USA (PST)
The samba was clearly inspired
by the margarita.
| |
| Hans Herrmann 2005-06-05, 8:57 pm |
|
"Darren New" <dnew@san.rr.com> schrieb im Newsbeitrag
news:68Ioe.41400$ya2.26287@tornado.socal.rr.com...
> Hans Herrmann wrote:
>
> Here, pos takes on the values of "l" and "b".
>
>
> So here, you're substituting periods for commas in "l" and "b", and
> assigning the result to pos.
>
> What you probably want is another level of look-up-the-variable, so the
> regsub line should be
>
> regsub -all {,} [set $pos] {.} [set pos]
>
> The first time thru the loop, [set pos] returns "l" and [set $pos] returns
> "1,0". SO this translates to
> regsub -all , 1,0 . l
> which is what you want.
>
> Basically, when you're looping over the names of variables, you have to
> put an extra indirection in there.
>
> HTH.
>
Hi Darren,
many thanks for your help!
Hans
| |
| Simon Geard 2005-06-06, 8:57 am |
| Hans Herrmann wrote:
> Hi all,
> I know its simple but ...???
>
> set l 1,0
> set b 2,0
>
Are you trying to solve a localization issue here? Is it that you've
read in '1,0' ( == 1.0 == 1·0 ) and want it interpreted as a real? If so
I don't know how to do it but I would certainly like to know how to get
tcl to interpret these equivalent forms as reals.
Simon Geard
| |
| Glenn Jackman 2005-06-06, 8:58 pm |
| At 2005-06-05 03:12PM, Darren New <dnew@san.rr.com> wrote:
> Hans Herrmann wrote:
>
> Here, pos takes on the values of "l" and "b".
>
>
> So here, you're substituting periods for commas in "l" and "b", and
> assigning the result to pos.
>
> What you probably want is another level of look-up-the-variable, so the
> regsub line should be
>
> regsub -all {,} [set $pos] {.} [set pos]
>
> The first time thru the loop, [set pos] returns "l" and [set $pos]
> returns "1,0". SO this translates to
> regsub -all , 1,0 . l
> which is what you want.
>
> Basically, when you're looping over the names of variables, you have to
> put an extra indirection in there.
Alternatively, loop over the substituted values:
foreach pos [list $l $b] {
#...
}
--
Glenn Jackman
NCF Sy min
glennj@ncf.ca
| |
| Hans Herrmann 2005-06-07, 4:03 pm |
|
"Simon Geard" <simon@quintic.co.uk> schrieb im Newsbeitrag
news:42a40276$0$1717$ed2e19e4@ptn-nntp-reader04.plus.net...
> Hans Herrmann wrote:
>
> Are you trying to solve a localization issue here? Is it that you've read
> in '1,0' ( == 1.0 == 1·0 ) and want it interpreted as a real? If so I
> don't know how to do it but I would certainly like to know how to get tcl
> to interpret these equivalent forms as reals.
>
>
> Simon Geard
Thanks to all!
You are very kind!
Hans
| |
| Hans Herrmann 2005-06-07, 8:58 pm |
|
"Glenn Jackman" <xx087@freenet.carleton.ca> schrieb im Newsbeitrag
news:slrnda8nb8.5dj.xx087@smeagol.ncf.ca...
> At 2005-06-05 03:12PM, Darren New <dnew@san.rr.com> wrote:
> Alternatively, loop over the substituted values:
> foreach pos [list $l $b] {
> #...
> }
Hi Glenn,
thanks for your answer but it doesn't work!
######
set a 1,0
set b 2,0
foreach pos [list $a $b] {
regsub -all {,} $pos {.} pos
}
# ... result
set a -> 1,0
set b -> 2,0
# ???
Hans
| |
| Glenn Jackman 2005-06-07, 8:58 pm |
| At 2005-06-07 11:43AM, Hans Herrmann <lieber@doc-herrmann.de> wrote:
> "Glenn Jackman" <xx087@freenet.carleton.ca> schrieb im Newsbeitrag
> news:slrnda8nb8.5dj.xx087@smeagol.ncf.ca...
>
>
> Hi Glenn,
> thanks for your answer but it doesn't work!
>
> ######
> set a 1,0
> set b 2,0
>
> foreach pos [list $a $b] {
> regsub -all {,} $pos {.} pos
> }
> # ... result
> set a -> 1,0
> set b -> 2,0
> # ???
Ah, sorry, I missed the point that you wanted to alter the variables
in-place. In that case, Darren's advice is the way to go.
foreach pos_var {a b} {
regsub -all {,} [set $pos_var] {.} $pos_var
puts "$pos_var -> [set $pos_var]"
}
Another approach to [regsub] is [string map]:
set a 1.000,05
set b 432.234,99
foreach pos_var {a b} {
# tranlate comma to dot and dot to comma
set $pos_var [string map {, . . ,} [set $pos_var]];
puts "$pos_var -> [set $pos_var]"
}
--
Glenn Jackman
NCF Sy min
glennj@ncf.ca
| |
| Hans Herrmann 2005-06-07, 8:58 pm |
|
"Glenn Jackman" <xx087@freenet.carleton.ca> schrieb im Newsbeitrag
news:slrndabhqg.iom.xx087@smeagol.ncf.ca...
> At 2005-06-07 11:43AM, Hans Herrmann <lieber@doc-herrmann.de> wrote:
>
> Ah, sorry, I missed the point that you wanted to alter the variables
> in-place. In that case, Darren's advice is the way to go.
>
> foreach pos_var {a b} {
> regsub -all {,} [set $pos_var] {.} $pos_var
> puts "$pos_var -> [set $pos_var]"
> }
>
> Another approach to [regsub] is [string map]:
> set a 1.000,05
> set b 432.234,99
> foreach pos_var {a b} {
> # tranlate comma to dot and dot to comma
> set $pos_var [string map {, . . ,} [set $pos_var]];
> puts "$pos_var -> [set $pos_var]"
> }
>
Hi Glenn,
thanks :-)
Hans
| |
| Schelte Bron 2005-06-08, 8:59 am |
| On 06/07/05 18:11, Glenn Jackman wrote:
> Ah, sorry, I missed the point that you wanted to alter the variables
> in-place. In that case, Darren's advice is the way to go.
>
> foreach pos_var {a b} {
> regsub -all {,} [set $pos_var] {.} $pos_var
> puts "$pos_var -> [set $pos_var]"
> }
>
> Another approach to [regsub] is [string map]:
> set a 1.000,05
> set b 432.234,99
> foreach pos_var {a b} {
> # tranlate comma to dot and dot to comma
> set $pos_var [string map {, . . ,} [set $pos_var]];
> puts "$pos_var -> [set $pos_var]"
> }
>
All these [set $pos_var] commands can get a bit confusing. Another
possibility I haven't seen mentioned sofar is to use upvar. I especially
find this method more convenient when you need to use the variable more
than once or twice inside the loop.
foreach varname {l b} {
upvar 0 $varname var
regsub -all {,} $var {.} var
puts "$varname -> $var"
}
Schelte
--
set Reply-To [string map {nospam schelte} $header(From)]
|
|
|
|
|