first, note that
%hash{'key1'}{'key2'} should be
$hash{'key1'}{'key2'}
If you're not accessing the hash through it's key order of key1, key2, then you should probably re-hash it. not sure what you're looking for, but this will make a hash keyed on 'string' and has an arrayref of the key2's. (no clue if your string:key2 is 1:1 or 1:many)
my %strToKey2Hash;
foreach my $href ( values %hash ){
while( my ($key2,$string) = each %$href ){
push @{$strToKey2Hash{$string}}, $key2;
}
}