Reputation: 542
Hi i have an array with basic key-value pair and i want to sort it and then get the keys. The array contents are something like this:
arrVal {
123adf45ad ABCD.1
aldfhr435d ABCD.9
lkh45q9er4 ABCD
1234dartf4 ABCD4
}
So basically my array keys are some kind of encrypted ids and the values are a string values. Since i cannot sort array based on its keys i did something like this:
foreach {key in array..} {
lappend mylist [$arrVal($key)];
}
set mylist [lsort $mylist];
now for the sorted list i want to extract the array keys against those values. I couldn't find any command that can extract the array keys based on its values. Any help?
Upvotes: 2
Views: 9536
Reputation: 246877
Here's a more step-by-step method:
array set arrVal {
123adf45ad ABCD.1
aldfhr435d ABCD.9
lkh45q9er4 ABCD
1234dartf4 ABCD4
}
set arrValList [list]
foreach {key val} [array get arrVal] {lappend arrValList [list $key $val]}
set sortedKeys [list]
foreach pair [lsort -index 1 $arrValList] {lappend sortedKeys [lindex $pair 0]}
puts $sortedKeys ;# lkh45q9er4 123adf45ad aldfhr435d 1234dartf4
Upvotes: 1
Reputation: 1798
You can get a list of all the keys in an array with array names.
set mylist [lsort [array names arrVal]]
Upvotes: 0
Reputation: 113926
Basically you're almost there. What you want is a reverse map which is not something built into the language (or indeed most languages) but rather a concept: another array with the values of the first array as keys and keys of the first array as values:
array set reverseMap {}
foreach {key val} [array get arrayVal] {
set reverseMap($val) $key
}
Now you can get the keys like this:
set secondKey $reverseMap([lindex $mylist 1])
Upvotes: 3