Skip to content

Instantly share code, notes, and snippets.

@tobyink

tobyink/frequency-sort.pl

Last active Oct 14, 2020
Embed
What would you like to do?
frequency sort
use strict;
use warnings;
use List::MoreUtils qw( frequency part uniq );
my @words = map /([\w']+)/g, <DATA>;
my %freq = frequency @words;
my @parts = part { $freq{$_} } uniq @words;
for my $i ( 0 .. $#parts ) {
next unless $parts[$i];
print "$i: ", join( " ", @{ $parts[$i] } ), "\n";
}
__DATA__
West Side Story
The award-winning adaptation of the classic romantic tragedy
"Romeo and Juliet". The feuding families become two warring
New York City gangs, the white Jets led by Riff and the Latino
Sharks, led by Bernardo. Their hatred escalates to a point
where neither can coexist with any form of understanding. But
when Riff's best friend (and former Jet) Tony and Bernardo's
younger sister Maria meet at a dance, no one can do anything
to stop their love. Maria and Tony begin meeting in secret,
planning to run away. Then the Sharks and Jets plan a rumble
under the highway--whoever wins gains control of the streets.
Maria sends Tony to stop it, hoping it can end the violence.
It goes terribly wrong, and before the lovers know what's
happened, tragedy strikes and doesn't stop until the
climactic and heartbreaking ending.
1: West Side Story award winning adaptation classic romantic Romeo Juliet feuding families become two warring New York City gangs white Riff Latino Bernardo Their hatred escalates point where neither coexist with any form understanding But when Riff's best friend former Jet Bernardo's younger sister meet at dance no one do anything their love begin meeting in secret planning run away Then plan rumble under highway whoever wins gains control streets sends hoping end violence It goes terribly wrong before lovers know what's happened strikes doesn't until climactic heartbreaking ending
2: The tragedy Jets led by Sharks it
3: of a can Tony Maria stop
4: to
9: the and
@smonff

This comment has been minimized.

Copy link

@smonff smonff commented Oct 14, 2020

Hi Toby.

I wrote a blog post about the __DATA__ token yesterday and this is an interesting related example (also List::*Util* use). I always use those kind of scripts at work to treat data for tasks colleagues who don't know how to program ask me about and I wish we could take some hours as a training so their life could be improved by reducing the use of painful Excel experiments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.