in reply to Sorting with perl

#!/usr/bin/perl use 5.8.0; use strict; use warnings 'all'; use sort 'stable'; my ($first, $second) = @ARGV; open my $fh => $first or die "open first: $!\n"; chomp (my @words = <$fh>); close $fh; my $c = 0; my %words = map {$_ => ++ $c} @words; open my $sh => $second or die "open second: $!\n"; my @info; while (<$sh>) { chomp; push @info => [split /\|/]; } close $sh; @info = sort {$words {$a -> [0]} <=> $words {$b -> [0]}} @info; print "@$_\n" for @info; __END__
Give the names of the two files as arguments. And if there's a lot of data, you may want to turn the sort into a GRT.

Abigail

Replies are listed 'Best First'.
Re: Re: Sorting with perl
by fruiture (Curate) on Aug 12, 2002 at 13:14 UTC

    Well, if you think the same, i can't be too wrong ;)

    --
    http://fruiture.de
      The example given suggests he wanted a stable sorting, hence my "use sort 'stable'". Your code only gives a stable sorting because that happens to be the default on 5.8.0, but that may change in a future version of perl (and if a sort happens to be stable in pre-5.8.0, it's just a coincidence).

      Abigail

        In fact, I didn't even think of the stability of the sort, assuming all keys would be mentioned in the guide-file anyway.

        --
        http://fruiture.de