#! perl -slw use strict; use Data::Dumper; my $data = do{ local $/; }; my %hash = eval "$1" if $data =~ m[ \n%some_hash\s*=\s* ( \( .* \); ) ]sx; print Dumper \%hash; my %newHash; for my $state (keys %hash ) { push @{ $newHash{ $_ } }, $state for split /\s*,\s*/, $hash{ $state }{colors}; } print Dumper \%newHash; __DATA__ Various other perl statements before the hash definition Various other perl statements before the hash definition Various other perl statements before the hash definition Various other perl statements before the hash definition Various other perl statements before the hash definition Various other perl statements before the hash definition %some_hash = ( 'NEW_YORK' => { 'capital' => 'Albany', 'colors' => 'black, green, blue', 'bird' => 'pigeon' }, 'MASSACHUSETTS' => { 'capital' => 'Springfield', 'colors' => 'red, white, blue', 'bird' => 'turkey' } # etc ); Various other perl statements after the hash definition Various other perl statements after the hash definition Various other perl statements after the hash definition Various other perl statements after the hash definition Various other perl statements after the hash definition Various other perl statements after the hash definition Various other perl statements after the hash definition #### P:\test>277327 $VAR1 = { 'MASSACHUSETTS' => { 'bird' => 'turkey', 'colors' => 'red, white, blue', 'capital' => 'Springfield' }, 'NEW_YORK' => { 'bird' => 'pigeon', 'colors' => 'black, green, blue', 'capital' => 'Albany' } }; $VAR1 = { 'green' => [ 'NEW_YORK' ], 'white' => [ 'MASSACHUSETTS' ], 'black' => [ 'NEW_YORK' ], 'blue' => [ 'MASSACHUSETTS', 'NEW_YORK' ], 'red' => [ 'MASSACHUSETTS' ] };