sub search_and_replace_in_hash { # Given a hash, perform a perl regexp operation on every value, regardless of how deep the hash is # Usage: search_and_replace_in_hash <\%hash> # Returns: New hash reference with replacement performed on deepest elements my %hash = %{shift()}; my $op = shift(); foreach my $key (sort keys %hash) { if ( reftype( $hash{$key} ) eq "HASH" ) { if (scalar( keys( %{$hash{$key}} ) ) > 0) { %hash = %{search_and_replace_in_hash( $hash{$key}, $op )}; } next; } if ( reftype( $hash{$key} ) eq "ARRAY" ) { if (scalar( @{$hash{$key}} ) > 0) { foreach my $list_element (@{$hash{$key}}) { if (reftype($list_element) eq "HASH") { %hash = %{search_and_replace_in_hash( $hash{$key}{$list_element}, $op )}; next; } } } else { next; } } # If you get here, you're a scalar %hash{$key} =~ $op; } return \%hash; }