
Untitled
By: a guest on
May 28th, 2012 | syntax:
None | size: 0.99 KB | hits: 12 | expires: Never
Perl Frequency sorting and other things
#! /usr/bin/perl
use utf8;
use warnings;
print "Please enter the name of the file: n" ;
$file = <STDIN>;
chop $file;
open(my $DATA, "<:utf8", $file) or die "Oops!!: $!";
binmode STDOUT, ":utf8";
while(<$DATA>) {
tr/A-Za-z//cs;
s/[;:()".,!?]/ /gio;
foreach $word (split(' ', lc $_)) {
$freq{$word}++;
}
}
foreach $word (sort { $freq{$b} <=> $freq{$a} } keys %freq) {
@fr = (@fr, $freq{$word});
@ord = (@ord, $word);
}
for ($v =0; $v < 10; $v++){
print " $fr[$v] | $ord[$v]n";
}
foreach $word (sort keys %freq) {
print "$word $freq{$word}n";
}
foreach $word (sort { $freq{$a} <=> $freq{$b} } keys %freq) {
print "$word $freq{$word}n";
}
open(my $DATA, "<:utf8", $file) or die "Oops!!: $!";
binmode STDOUT, ":utf8";
perl -CS -F\PL+ -alne'$f{lc$_}++for@F}{print"$f{$_} $_"for sort{$f{$b}<=>$f{$a}}keys%f'