Reputation: 43
I've created variables based off a array and then populated that variable with the results of that array and trying to access that variable in a loop to replace text within a string.
I have an array
mookie profile:alias:0 mookie
mookie profile:class:0 hacker
mookie profile:inventory:0 DigitalHackerDevice
mookie profile:inventory:1 BootDisk
mookie profile:inventory:2 HackThePlanet99
and I get all of the array and store it
#get_userSprite is defined via an HTML form so "mookie" for this example
set array "[lsort -dictionary [array names $get_userSprite]]"
I have a loop which populates $ with the key (alias, class, inventory)
foreach item $array {
set datastoreKey [lindex [string map {":" " "} $item] 1] ;#alias, class, inventory
#using the array string profile:alias:0 get the item
set array_string [getItem $get_userSprite $item] ;#get the result for profile:alias:0 returns "mookie"
lappend $datastoreKey "$array_string" ;# this then creates variables with the result of that.
#$alias eq mookie
#$class eq hacker
#$inventory eq item1,item2,item3
} ;#close loop
I can confirm these are populated
puts $class ;# returns mookie
puts $alias ;# returns merchant
puts $inventory ;# returns DigitalHackerDevice, item1, item2
I've also stored a list of the three fields I wish to access
if { $datastoreKey ni $myList } { lappend myList [list $datastoreKey] } else { puts "Don't append" }
so myList now contains: alias class inventory
I have a tagswap procedure which replaces "word with string" in a variable
proc tagSwap {craft values} { return [string map $values $craft] } ;#Preforms morph of words.
so
foreach item $myList {
puts $item ;#alias class inventory
set swaps [tagSwap $string [list "$item" "$item"]]
} ;#end loop
I'm dumbstruck on how to how to access the contents of $alias to replace the word "alias" with "mookie" in the string because $item is "alias" and it replaces it with that and so on for the rest. Class, Inventory etc..
Upvotes: 0
Views: 106
Reputation: 4813
Your results do not match the input data you provided. Ignoring that, your question seems to boil down to: I have a variable "item", containing the name of another variable. How do I dump the value of that other variable?
While this can be done using puts [set $item]
, it is usually easier to change your code to use array members instead of scalar variables.
So instead of lappend $datastoreKey "$array_string"
, use lappend datastore($datastoreKey) "$array_string"
. Then getting the value of the item is just a matter of: puts $datastore($item)
This also avoids the risk of any item name clashing with variables you use in your code.
Upvotes: 1