http://www.perlmonks.org?node_id=626764


in reply to Re: Counting the Times Two Unknown Values Appear Together
in thread Counting the Times Two Unknown Values Appear Together

I'd simplify it a bit further:

my %count; while (<FILE>) { my( $src, $sport ) = (split /;/)[9,12]; $count{$src,$sport}++; # magic $; } for my $pair ( sort keys %count ) { print "($pair) occurred on a line together $count{$pair} times\n"; # if you need the two parts: # my( $src, $sport ) = split $;, $pair; }
In fact, I'd extend it to any number of fields:
my @fields = ( 9, 12 ); my %count; while (<FILE>) { chomp; $count{ (split /;/)[@fields] }++; } for ( sort keys %count ) { print "($_) occurred on a line together $count{$_} times\n"; # if you need the parts: # my @vec = split $;; }