in reply to Pattern-matching hash keys

You don't indicate how big your %images hash is likely to be, but if it will be more than a few 10s of entries, doing a linear search each time using grep and a regex to test if the elements you are after exists is going to become time consuming.

If your keys will always have a consistant format (as indicated by your first two) of "copy_n_frag_n_id", then most of that information is redundantly duplicated. The only valuable information are the two numbers. Normally, when tables are index by numbers I would think of an array, or in this case an AoAs, but the test for existance suggests that your data is probably sparse. In this case, using a HoHs, with numeric keys allows you to build a sparse data structure whilst retaining the ability to go straight to the entry of choice avoiding the linear search.

By way of demonstration.

#! perl -slw use strict; use Data::Dumper; my %images; $images{$1}{$2} = undef while <DATA> =~ m[copy_(\d+)_frag_(\d+)_id]; print Dumper \%images; for my $copy ( 1 .. 9 ) { for my $frag ( 1 .. 9 ) { $images{$copy}{$frag} = int rand 100000 if exists $images{$copy} and exists $images{$copy}{$frag}; } } print Dumper \%images; __DATA__ copy_1_frag_1_id copy_1_frag_2_id copy_1_frag_3_id copy_1_frag_4_id copy_1_frag_5_id copy_1_frag_6_id copy_1_frag_7_id copy_1_frag_8_id copy_1_frag_9_id copy_2_frag_3_id copy_2_frag_5_id copy_2_frag_7_id copy_2_frag_9_id

Output

P:\test>test $VAR1 = { '1' => { '6' => undef, '3' => undef, '7' => undef, '9' => undef, '2' => undef, '8' => undef, '1' => undef, '4' => undef, '5' => undef }, '2' => { '3' => undef, '7' => undef, '9' => undef, '5' => undef } }; $VAR1 = { '1' => { '6' => 88293, '3' => 61117, '7' => 13104, '9' => 29397, '2' => 45159, '8' => 6976, '1' => 17465, '4' => 38931, '5' => 10040 }, '2' => { '3' => 73205, '7' => 8816, '9' => 71615, '5' => 88977 } };

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.