in reply to sorting a hash that's in another file

Assuming that you know the hash name, and that it is a real hash, not a reference, this one would work.

Caveat! Your example has one key named "color" and the other named "colors". You should unify them.

#!/usr/bin/perl -w use strict; use Data::Dumper; my $other = 'somescript.pl'; my $find = '%some_hash'; open SCRIPT, "< $other" or die "can't open\n"; my $first = ""; my $hash_text = ""; while (<SCRIPT>) { if (!$first) { $first = /$find/; } next unless $first; s/^\s*my $find/$find/; $hash_text .= $_; last if /\)/; } close SCRIPT; my %some_hash; # must be the same name as the hash in the other scrip +t eval $hash_text; my %colors ; for ( keys %some_hash) { for my $color ( split /,\s?/, $some_hash{$_}{colors}) { push @{$colors{$color}}, $_; } } print Data::Dumper->Dump([\%colors], ['colors']),$/; __END__ $colors = { 'blue' => [ 'MASSACHUSETTS', 'NEW_YORK' ], 'green' => [ 'NEW_YORK' ], 'black' => [ 'NEW_YORK' ], 'white' => [ 'MASSACHUSETTS' ], 'red' => [ 'MASSACHUSETTS' ] };

Notice that this code will break if your hash contains a literal ")". But just to give you the idea.