help
Rev | Line | |
---|
[f1563c0] | 1 | #! /usr/bin/perl |
---|
| 2 | |
---|
| 3 | use locale; |
---|
| 4 | use strict; |
---|
| 5 | |
---|
| 6 | my @prefs; |
---|
| 7 | |
---|
| 8 | sub addPref { |
---|
| 9 | |
---|
| 10 | my $pref = shift; |
---|
| 11 | my $desc = shift; |
---|
| 12 | my $i; |
---|
| 13 | for ($i=0; $i< @prefs; ++$i) { |
---|
| 14 | my @tab = @{$prefs[$i]}; |
---|
| 15 | if (${@{$prefs[$i]}}[0] =~ /^$pref/) { |
---|
| 16 | ${@{$prefs[$i]}}[1]{$desc}++; |
---|
| 17 | return; |
---|
| 18 | } |
---|
| 19 | } |
---|
| 20 | my @new; |
---|
| 21 | my %hash; |
---|
| 22 | |
---|
| 23 | $hash{$desc}++; |
---|
| 24 | push(@new, $pref); |
---|
| 25 | push(@new, \%hash); |
---|
| 26 | |
---|
| 27 | push(@prefs, \@new); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | sub printPrefs { |
---|
| 31 | |
---|
| 32 | my $i; |
---|
| 33 | for $i (@prefs) { |
---|
| 34 | my @tab = @$i; |
---|
| 35 | # print $tab[0]."\t"; |
---|
| 36 | my $pref = $tab[0]; |
---|
| 37 | my %hash = %{$tab[1]}; |
---|
| 38 | my @keys = keys(%hash); |
---|
| 39 | # print(@keys."\n"); |
---|
| 40 | my $sum = 0; |
---|
| 41 | my $key; |
---|
| 42 | for $key (@keys) { |
---|
| 43 | $sum += $hash{$key}; |
---|
| 44 | } |
---|
| 45 | for $key (@keys) { |
---|
| 46 | print $pref."\t"; |
---|
| 47 | print $key."\t"; |
---|
| 48 | print $hash{$key}."\t"; |
---|
| 49 | print $sum."\n"; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | if (@ARGV < 2) { |
---|
| 55 | print "USAGE: count_prefs.pl MIN_PREF_LEN MAX_PREF_LEN\n"; |
---|
| 56 | exit; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | my $MIN = shift; |
---|
| 60 | my $MAX = shift; |
---|
| 61 | my $PART = shift; |
---|
| 62 | |
---|
| 63 | if ($MIN > $MAX) { |
---|
| 64 | print "MIN_PREF_LEN > MAX_PREF_LEN! ($MIN > $MAX)\n"; |
---|
| 65 | exit; |
---|
| 66 | } |
---|
| 67 | my $begin = ""; |
---|
| 68 | while (<>) { |
---|
| 69 | my $len = $MIN; |
---|
| 70 | $_ =~ /(\w+);(.*)$/; |
---|
| 71 | my $pref = $1; |
---|
| 72 | my $desc = $2; |
---|
| 73 | if ($begin eq "") { |
---|
| 74 | $begin = substr($pref, 0, $MIN); |
---|
| 75 | } |
---|
| 76 | if ($pref !~ /^$begin.*/) { |
---|
| 77 | printPrefs(); |
---|
| 78 | undef(@prefs); |
---|
| 79 | $begin = ""; |
---|
| 80 | } |
---|
| 81 | while ($len <= $MAX) { |
---|
| 82 | addPref(substr($pref, 0, $len++), $desc); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | printPrefs(); |
---|
Note: See
TracBrowser
for help on using the repository browser.