Friday Perl Challenge
Jonathan McKeown
jonathan at hst.org.za
Fri Apr 20 11:15:25 BST 2007
On Friday 20 April 2007 11:38, Jonathan McKeown wrote:
> On Friday 20 April 2007 09:24, Chris Jack wrote:
> > In one line. write a script to read stdin (or variable number of file
> > arguments) that splits the input into words (non-whitespace consecutive
> > characters), and outputs, sorted, each unique word and the number of
> > times it occurs.
>
> perl -alne 'map{$_++}@c{@F}}{print "$_ $c{$_}" for sort keys %c'
>
> Ugly map in void context to avoid using foreach twice.
And do I get a bonus point for
perl -alne '$t+=map{$_++}@c{@F}}END{print$t}{print"$_ $c{$_}"for sort keys %c'
which uses map in scalar context instead and also prints the total number of
words at the end?
Jonathan
More information about the london.pm
mailing list