First Perl6 program

fw on 2008-12-09T21:43:07

This one reads STDIN, counts words (here words are things between whitespace), and prints them sorted in descending order by occurrence.

my %words;

$*IN.lines.split(/\s+/).map: { %words{$_}++ };

for %words.pairs.sort: { $^b.value <=> $^a.value } -> $pair { say $pair }


see also: this node at perlmonks


Sweet!

educated_foo on 2008-12-09T22:51:15

It's like longhand for

perl -lne '$x{$_}++ for split; END { print "$_\t$x{$_}" for sort { $x{$a} $x{$b} } keys %x }

Re:Sweet!

educated_foo on 2008-12-09T22:54:11

Ugh. $x{$b} <=> $x{$a}.

Re: First Perl6 program

daxim on 2010-07-01T09:37:25

The program is inefficient. See http://stackoverflow.com/questions/3135673#comment-3229177 et sqq. and http://justrakudoit.wordpress.com/2010/06/30/rakudo-and-speed/.