Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 28th, 2012  |  syntax: None  |  size: 0.99 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Perl Frequency sorting and other things
  2. #! /usr/bin/perl
  3.  
  4. use utf8;
  5. use warnings;
  6.  
  7. print "Please enter the name of the file: n" ;
  8. $file = <STDIN>;
  9. chop $file;
  10.  
  11.  
  12. open(my $DATA, "<:utf8", $file) or die "Oops!!: $!";
  13. binmode STDOUT, ":utf8";
  14.  
  15. while(<$DATA>) {                          
  16.     tr/A-Za-z//cs;
  17.     s/[;:()".,!?]/ /gio;                  
  18.     foreach $word (split(' ', lc $_)) {  
  19.     $freq{$word}++;                  
  20.      }
  21. }
  22. foreach $word (sort { $freq{$b} <=> $freq{$a} } keys %freq) {          
  23.    @fr = (@fr, $freq{$word});
  24.    @ord = (@ord, $word);        
  25. }
  26. for ($v =0; $v < 10; $v++){
  27. print " $fr[$v] | $ord[$v]n";
  28. }
  29.        
  30. foreach $word (sort keys %freq) {        
  31.     print "$word $freq{$word}n";        
  32. }
  33.        
  34. foreach $word (sort { $freq{$a} <=> $freq{$b} } keys %freq) {        
  35.     print "$word $freq{$word}n";
  36. }
  37.        
  38. open(my $DATA, "<:utf8", $file) or die "Oops!!: $!";
  39. binmode STDOUT, ":utf8";
  40.        
  41. perl -CS -F\PL+ -alne'$f{lc$_}++for@F}{print"$f{$_} $_"for sort{$f{$b}<=>$f{$a}}keys%f'