Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

foreach problem
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



Report this thread to moderator Post Follow-up to this message
Old Post
Hans Herrmann
06-06-05 01:57 AM


Re: foreach problem
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Darren New
06-06-05 01:57 AM


Re: foreach problem
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Hans Herrmann
06-06-05 01:57 AM


Re: foreach problem
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

Report this thread to moderator Post Follow-up to this message
Old Post
Simon Geard
06-06-05 01:57 PM


Re: foreach problem
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 Symin
glennj@ncf.ca

Report this thread to moderator Post Follow-up to this message
Old Post
Glenn Jackman
06-07-05 01:58 AM


Re: foreach problem
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Hans Herrmann
06-07-05 09:03 PM


Re: foreach problem
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Hans Herrmann
06-08-05 01:58 AM


Re: foreach problem
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 Symin
glennj@ncf.ca

Report this thread to moderator Post Follow-up to this message
Old Post
Glenn Jackman
06-08-05 01:58 AM


Re: foreach problem
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Hans Herrmann
06-08-05 01:58 AM


Re: foreach problem
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)]

Report this thread to moderator Post Follow-up to this message
Old Post
Schelte Bron
06-08-05 01:59 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Tcl archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:44 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.