I had a few spare moments, and tried using your code
shotgunefx. You have a curly brace at the end of your second line and I assume you mean a paren.
Here's some test code to prove my point:
#!/usr/bin/perl
use strict;
use warnings;
my %h1;
my %h2;
my %h3;
for( my $count = 0, my $char = 'a'; $char ne 'aaaa'; $count++, $char++
+ )
{
# print "Adding $char with value of $count to h1\n";
$h1{ $char } = $count;
}
foreach my $key ( keys %h1)
{
$h2{ $key } = $h1{$key};
}
foreach my $key (reverse sort keys %h1)
{
$h3{ $key } = $h1{$key};
}
#foreach my $key (sort keys %h1) { print "h1: key: $key value: $h1{
+$key}\n";}
#foreach my $key (sort keys %h2) { print "h2: key: $key value: $h2{
+$key}\n";}
#foreach my $key (sort keys %h3) { print "h3: key: $key value: $h2{
+$key}\n";}
my ($k1,$k2) = ( join('',sort keys %h1), join('',sort keys %h2) );
my ($k3,$k4) = ( join('',keys %h1), join('',keys %h2) );
my ($k5,$k6) = ( join('',sort keys %h1), join('',sort keys %h3) );
my ($k7,$k8) = ( join('',keys %h1), join('',keys %h3) );
print "inserted same order, keys sorted compare: " . ($k1 eq $k2 ? 1 :
+ 0) . "\n";
print "inserted same order, keys not sorted compare: " . ($k3 eq $k4 ?
+ 1 : 0) . "\n";
print "inserted reverse order, keys sorted compare: " . ($k5 eq $k6 ?
+1 : 0) . "\n";
print "inserted reverse order, keys not sorted compare: " . ($k7 eq $k
+8 ? 1 : 0) . "\n";
__DATA__
Prints:
inserted same order, keys sorted compare: 1
inserted same order, keys not sorted compare: 0
inserted reverse order, keys sorted compare: 1
inserted reverse order, keys not sorted compare: 0
So to answer your question about sorting, the sort of the keys is necessary or the compare will not work.