This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Manipulating multiple variables simultaneously

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Manipulating multiple variables simultaneously

Post by arfer »

I want to trim multiple variables in a single line of code. For example, trimming the caret symbol (^) from the start and end of the variables a, b, c, d, e, f as below :-

Code: Select all

foreach var {a b c d e f} {set $var [string trim $$var ^]}
The second $ in $$var doesn't get substituted. It behaves as if I had used ${$var}. Is there any 'neat/clever' way to do this without resorting to the following code :-

Code: Select all

foreach var {a b c d e f} {set $var [string trim $[subst -nocommands -nobackslashes $var] ^]}
The problem being any dollar symbol in any one of more of the variable values will probably break the code.
I must have had nothing to do
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

use set

Post by user »

Code: Select all

set $var [string trim [set $var] ^]
Have you ever read "The Manual"?
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

Thanks!

Though I feel a bit of a fool not managing to work that one out myself.
I must have had nothing to do
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I suppose you could use upvar for this, but otherwize user's suggestion should do just well...
NML_375
Post Reply