Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Sorting with perl

by Abigail-II (Bishop)
on Aug 12, 2002 at 13:00 UTC ( [id://189471]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://189471]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-19 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found