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

Variables with the same name in one list - possible?

Status
Not open for further replies.

yodaa

Programmer
Jun 10, 2004
64
SE
Hi,

Is is possible to put some variables into a list that has the exact same variable name but different contents.

E.g.
Code:
#Persons list
list persons {}

#Person
set person(name) "Jack"
set person(lname) "Jackson"

#Add to list
lappend persons person

#Next person
set person(name) "John"
set person(lname) "Johnson"

#Add to list
lappend persons person
Possible?
 
Not the way you have it set up now. You are appending the word, "person" to the list, "persons". Not very useful. You could "lappend persons {$person(name) $person(lname)}" .

I think it would be better, though, to make person a list. So that you would "set person {John Johnson}". Then you can "lappend persons $person"

Bob Rashkin
rrashkin@csc.com
 
Bob,

The "set person {John Johnson}" combined with "lappend persons $person" is a good way, thanks..
But clumsy me gave you to little information of my problem.. My list will not contain just the "person" elements it will also contain information such as

project "Bal bla"
doc_ref "Report 1"
doc_ref "Report 2"

How do I get all "person"-elements from my list since the list will contain a mixed content? Is there a way of naming the elements so that I just can extract the elements i wish for?
 
Maybe you want a combination of list and array. For an experiment, make an array and then do: array get <array name>. The result will be a list with alternating index/value pairs, where the index is a string that may include multiple indices (e.g. 1,project). So you can build your list with indices:
"set person {0,name John 0,lname Johnson 0,project "Bla bla"}"; lappend persons $person.
Then you can use the list to initialize an array:
"array set persons"
Then you can refer to elements by index.

Bob Rashkin
rrashkin@csc.com
 
Bob's right, but this sounds like a perfect time to use an itcl class, especially if you have a number of variables to be set for each person. You'll find this to be a lot more scalable in the future as the definition of "person" changes.

 
Thanx for all the replies...

I've come up with a solution for now, not neat, but functional..

Thanx again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top