Hm. I'm still a little confused as to what's up. Your general matching approach seems okay, although I've noticed minor syntax errors (e.g., misspelled variable names, etc.) in each example you posted. I assumed that they were simply transcription errors, but that might be your problem.
Here's a cleaned up example of your previous code fragment, demonstrating that it matches up the keys properly:
[tt]%
parray arra
arra(1,file) = c:/filename
arra(2,file) = c:/file2
arra(3,file) = c:/abcfile
arra(4,file) = c:/toast
arra(5,file) = c:/waffles
arra(6,file) = c:/pancakes
arra(c:/abcfile) = 5434534
arra(c:/file2) = 58098245
arra(c:/filename) = 1234123
arra(c:/pancakes) = 12465436
arra(c:/toast) = 6345
arra(c:/waffles) = 47313
%
foreach val [array names arra c:/*] {
foreach val2 [array names arra *,file] {
if {[string match $val $arra($val2)]} {
puts [format "%-7s %-12s : %s" $val2 $val $arra($val)]
}
}
}
3,file c:/abcfile : 5434534
6,file c:/pancakes : 12465436
5,file c:/waffles : 47313
1,file c:/filename : 1234123
4,file c:/toast : 6345
2,file c:/file2 : 58098245[/tt]
Now, if what you're really trying to do is process the files "1,file 2,file...", you should approach this a bit differently. First, I'd use
array names to get the list of "*,file" keys. However, because Tcl arrays are unordered data structures (the information is actually stored in a hash table internally), if you want to process these values in the order "1,file 2,file...", you need to pass this list to
lsort; this is a case where you should realy use the
lsort -dictionary option to correctly sort using the numerical prefix, rather than simply treating the numerals as ASCII characters.
For each index, you can then test to see if a corresponding element exists in your array using
info exists, and if it does, process it. Putting all that together, we've got:
[tt]%
foreach key [lsort -dictionary [array names arra *,file]] {
if {[info exists arra($key)]} {
puts [format "%-7s %-12s : %s" $key $arra($key) $arra($arra($key))]
}
}
1,file c:/filename : 1234123
2,file c:/file2 : 58098245
3,file c:/abcfile : 5434534
4,file c:/toast : 6345
5,file c:/waffles : 47313
6,file c:/pancakes : 12465436[/tt]
Did that accomplish more or less what you were looking for? If not, feel free to clarify, and I'll give it another go.
And now a couple more general comments...
In general, if the order of elements is important, I tend to store information in a list rather than an array. In Tcl, a list is an ordered structure by nature, whereas imposing order on array elements requires additional work. As of Tcl 8.0, list access performance is roughly the same as array access until you start dealing with thousands of elements. Tcl lists are also "first class" data types that you can pass by value to procedures, whereas Tcl arrays aren't, and require the use of
upvar like you used above. But sometimes the benefits of using arrays (for example, being able to use the glob pattern argument to
array names to filter elements) outweigh the inconveniences.
Another comment, for simple string comparisons, use
string equal insead of
string match (as long as you're using Tcl version 8.1.1 or later, where
string equal was introduced). It's faster, and you don't need to worry about wildcard characters like "*" and "?" possibly appearing in your comparison string. Use
string match only when you really want glob (shell-style) pattern matching. - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax