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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array question 2

Status
Not open for further replies.

zombieZero

Technical User
Apr 7, 2004
28
US
I searched around but I didn't find anything that quite seemed what I was looking for...this seems like it should be simple but I just can't figure it out;

I have an array and I want to use variables in it:

set ip [getL3Addr]
set name [getName]
set etc [getOther]
array set info {
test1 {$ip $name $etc}
}

...but whenever I query the array, I just get the var names:

puts $info(test1)
{$ip $name $etc}

I can manipulate the variables inside in other ways:

set info(test1) [lreplace $info(test1) 0 0 [getL3Addr]]

...but is there a simpler way? Is there some simple thing that I'm just missing that will allow me to just use the variables inside the array?

 
The problem is, as always, the curly braces and the delayed substitution they imply. Not having your proc's, I did this:
Code:
set ip 42
set name 43
set etc 44
from here on, it should be like what you need to do:
Code:
set pl "$ip $name $etc";# this puts the variables in a list
array set info "test1 {$pl}";# delays substitution only once
then,
% array get info
test1 {42 43 44}
% set info(test1)
42 43 44
%
looks good.


Bob Rashkin
rrashkin@csc.com
 
Thanks Bong - I can use that...but I still would like to be able to dynamically set a variable within an array and then retrieve it (if possible). Maybe there's a better way to do this, but in a nutshell, I'd like to be able to do this:

% array set infoArr {
test1 {$ip $mask $mac}
}
% set ip 10.1.1.1
10.1.1.1
% set mask 255.255.255.0
255.255.255.0
% set mac 000000112233
000000112233
% puts "THIS IS TEST1 IP: [lindex $infoArr(test1) 0]"
THIS IS TEST1 IP: $ip
%

...only instead of the last line returning $ip, I'd like it to return "10.1.1.1"


 
OK. If I understand correctly, you want something like what FORTRAN called "equivalence". I don't think Tcl supports that concept but you could probably achieve the same result with some proc's.

It seems you want to identify array elements with variable names before the values are assigned to the variables. Right? Well that's the sort of thing "upvar" does from a proc to a calling scope. That would mean assigning the local variables, say ip, name, etc, in the proc to the global scope (but not global) variable info(test1) with upvar. Then when you assign the variable value (in the proc) it should set correctly in the array.

Bob Rashkin
rrashkin@csc.com
 
Does
Code:
  array set infoArr [list test1  [list $ip $mask $mac]]
is what you're after?

You could also use format:
Code:
  array set [format {test1 {%s %s %s}} $ip $mask $mac]

More on format at:
HTH

ulis
 
By Jove, ulis...that did it! Not exactly sure how or why it does (your first example is pretty much what I was looking for), but this is good information.

Thanks ulis, and Bong!
 
How & why:

array set argument is a list built with names followed by values:
Code:
  array set [list name-1 value-1 ... name-n value-n]
The Tcl way to build a dynamic list is:
Code:
  ... [list item-1 ... item-n]
The Tcl way to build a constant list is:
Code:
  ... {item-1 ... item-n}
In fact, this way is building a string constant that (when needed) is evaled as a list constant by:
Code:
  ... [split {item-1 ... item-n}]
This was what you used. The pitfall is that (as Bong explaned) you got a constant list where you wanted a dynamic list.

HTH

ulis
 
I may not understand what you're looking for (AND this is quite LATE)...
% array set infoArr {
test1 {ip mask mac}
}
% set ip 10.1.1.1
10.1.1.1
% puts "Test1 IP: [lindex $infoArr(test1) 0]"
Test1 IP: ip
% set [lindex $infoArr(test1) 0]
10.1.1.1

... Maybe this does it. "ip mask & mac" are variable names. lindex of the array gives the name. set [name] gives the value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top