in reply to Comparing two arrays
this was freaking weird.. sorry at least for me- maybe it's my terrible hangover. but.. here goes..
#!/usr/bin/perl use strict; my @a1=qw(yo and this too too too a b c d d d d d); my @a2=qw(yo and too too x f 3 d d moon); my %a1=(); my %a2=(); for (@a1){ $a1{$_}++; #$found{$_}++; } for (@a2){ $a2{$_}++; #$found{$_}++; } for (keys %a1){ $a2{$_} or delete $a1{$_}; } for (keys %a2){ $a1{$_} or delete $a2{$_}; } # now a1 and a2 both contain the same keys, but possibly diff values my %highest=(); for (keys %a1){ # or a2, same thing if ($a1{$_} > $a2{$_} ){ $highest{$_}=$a1{$_}; } else { $highest{$_}=$a2{$_}; # also if they are the same, non higher +then the other } } for (keys %highest){ print "$_:$highest{$_}\n"; }
output is:
yo:1 d:5 and:1 too:3
hope that helps or amuses
|
|---|