Advertisement
cd62131

summarize frequency

Nov 16th, 2017
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.70 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. open my $in, '<', 'test.txt' or die;
  5. my $freq = [];
  6. my $word = [];
  7. while (<$in>) {
  8.   chomp;
  9.   my @line = split ',';
  10.   push @$word, @line;
  11.   for (my $i = 0; $i <= $#line; ++$i) {
  12.     $freq->[$i] or $freq->[$i] = {};
  13.     ++$freq->[$i]->{$line[$i]};
  14.   }
  15. }
  16. close $in;
  17. my %h = map { $_ => 1 } @$word;
  18. my @uniq_word = sort keys %h;
  19. my $all = {};
  20. for (my $i = 0; $i < @$freq; ++$i) {
  21.   print "column $i\n";
  22.   for (@uniq_word) {
  23.     $freq->[$i]->{$_} or $freq->[$i]->{$_} = 0;
  24.     $all->{$_} or $all->{$_} = 0;
  25.     $all->{$_} += $freq->[$i]->{$_};
  26.     print "$_ = $freq->[$i]->{$_}\n";
  27.   }
  28. }
  29. print "all\n";
  30. print "$_ = $all->{$_}\n" for (@uniq_word);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement