in reply to confused with a hash which contains reversed keys
The easiest way is to do it correctly from the beginning.
sub get_key { join '', sort $_[0] =~ /./g } my %hash; while (<DATA>) { chomp; $hash{get_key($_)}++; } use Data::Dumper; print(Dumper(\%hash)); __DATA__ AB AB AB AC AC BA BA BA CA
$VAR1 = { 'AC' => 3, 'AB' => 6 };
But the same method will allow you to fix your hash. (Added)
sub get_key { join '', sort $_[0] =~ /./g } my %hash; while (<DATA>) { chomp; $hash{$_}++; } my %fixed; foreach my $key (keys %hash) { my $val = $hash{$key}; $fixed{get_key($key)} += $val; } use Data::Dumper; print(Dumper(\%fixed)); __DATA__ AB AB AB AC AC BA BA BA CA
By the way, this is a moderated site, so your post may not appear instantly. Please don't triple post.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: confused with a hash which contains reversed keys
by Not_a_Number (Prior) on Dec 03, 2007 at 19:24 UTC | |
by ikegami (Patriarch) on Dec 03, 2007 at 22:15 UTC |