Jeremy Kister wrote:
I am trying to simplify some of my code.  The output I am expecting is:
1 17.0.0.1
5 27.0.0.1
5 127.0.0.1
5 209.0.0.1
6 10.0.0.1
10 127.0.1.1

where the first colomn is the main sorting column, and if there are
multiples of that value, the ip should be sorted.

I have code the performs the task, but it's a Swartzian Transform, a
temporary array, another swartz transform, and just feels icky.

any suggestions to pretty this bloat ?
>
#!/usr/local/bin/perl

$hash{'127.0.0.1'} = 5;
$hash{'127.0.1.1'} = 10;
$hash{'27.0.0.1'} = 5;
$hash{'10.0.0.1'} = 6;
$hash{'17.0.0.1'} = 1;
$hash{'209.0.0.1'} = 5;

my @ipsorted = map { $_->[0] }
               sort { $a->[1] <=> $b->[1] }
               map { [$_,split /\./] } keys %hash;

foreach my $ip (@ipsorted){
   push @unsorted, "$hash{$ip} $ip";
}

foreach my $sorted (map { $_->[0] }
                    sort { $a->[1] <=> $b->[1] }
                    map { [$_,split] } @unsorted){
   print "$sorted\n";
}


The Schwartzian Transform is good but it doesn't beat the Guttman Rosler Transform. :-)

#!/usr/local/bin/perl
use warnings;
use strict;

use Socket;

my %hash = (
    '127.0.0.1' => 5,
    '127.0.1.1' => 10,
    '27.0.0.1'  => 5,
    '10.0.0.1'  => 6,
    '17.0.0.1'  => 1,
    '209.0.0.1' => 5,
    );

print map /\0([^\0]+)\z/,
      sort
      map pack( 'n', $hash{ $_ } ) . inet_aton( $_ ) . "\0$hash{$_} $_\n",
      keys %hash;

__END__


John -- use Perl; program fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to