Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable manipulation problem

Status
Not open for further replies.

fhutt

Programmer
Jul 7, 2006
79
0
0
AU
I would like to modify the following code to make it work:
set i 0
set arg$i {C:/Program File/hell0}
regsub -all -- {/} $arg$i "\x5c" arg$i

I get an error when running this code for the regsub line saying "can't read arg, no such variable". This is true but arg0 does exist and $i is a 0.
Could someone please help with this?
Thanks
 
Hi fhutt,

I would do that using hash like this:

Code:
[COLOR=#804040][b]set[/b][/color] i [COLOR=#ff00ff]0[/color]
[COLOR=#804040][b]set[/b][/color] arg([COLOR=#008080]$i[/color]) {C:/Program File/hell0}
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"arg([/color][COLOR=#008080]$i[/color][COLOR=#ff00ff]) = [/color][COLOR=#008080]$arg[/color][COLOR=#ff00ff]([/color][COLOR=#008080]$i[/color][COLOR=#ff00ff])"[/color]
[COLOR=#804040][b]regsub[/b][/color] -all -- {/} [COLOR=#008080]$arg[/color]([COLOR=#008080]$i[/color]) [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\x5c[/color][COLOR=#ff00ff]"[/color] arg([COLOR=#008080]$i[/color])
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"arg([/color][COLOR=#008080]$i[/color][COLOR=#ff00ff]) = [/color][COLOR=#008080]$arg[/color][COLOR=#ff00ff]([/color][COLOR=#008080]$i[/color][COLOR=#ff00ff])"[/color]

Output:
Code:
C:\_mikrom\Work>tclsh fhutt02.tcl
arg(0) = C:/Program File/hell0
arg(0) = C:\Program File\hell0
 
Hi microm,
I had this problem before. I couldn't remember how I fixed it, so I did some trial and error. What I came up with is to replace $arg$i with [set arg$i]. Once I got this to work it came back to me that this is what I did before.
However, I prefer your more consistent solution.
Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top