'TCL hash wildcard usage?

Assume we have a 2D hash.

set a(1,x) abc
set a(2,x) def

Now I want to know if 'x' exists in both. I can do :

info exists a(1,x) && info exists a(2,x)

I can put this into a loop but that starts to look clunky.

Is there a way to wildcard ? This naive idea obviously doesn't work.

info exists a(*,x)

Any solution?

tcl


Solution 1:[1]

You want the array names command here:

set all_names [array names a]
set x_names [array names a "*,x"]    ;# quotes not strictly required here

if {[llength $all_names] == [llength $x_names]} {
    puts "all array keys have x"
} else {
    puts "some array keys don't have x"
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 glenn jackman