|
hi sekar --
In a message dated 5/31/2006 9:08:32 P.M. Eastern Standard Time,
[EMAIL PROTECTED] writes:
> How to identify a maximum occuring element in a Perl
Array
> > e.g @array values are CMSIF CMSIF CMSFV > > Since CMSIF has two occurences , I need to select CMSIF > > Any suggestions > > Thanks > > Sekhar maybe try something like the following command line examples (please
forgive line wrap noise;
drag box out horizontally for a little better viewing):
C:[EMAIL PROTECTED]>perl -we "use strict; my @array = qw(x CMSIF CMSIF CMSFV x
a b c d x e CMSIF);
my %hash; $hash{ $array[$_] }{f}++, $hash{ $array[$_] }{i} = $_ for reverse
(0 .. $#array);
my $max = (sort { $hash{$b}{f} <=> $hash{$a}{f} or $hash{$b}{i}
<=> $hash{$a}{i} } keys %hash)[0];
print qq(element $max at index $hash{$max}{i} is most frequent \n)"
element CMSIF at index 1 is most frequent C:[EMAIL PROTECTED]>perl -we "use strict; my @array = qw(x CMSIF CMSIF CMSFV x
a b c d x e CMSIF);
my %hash; $hash{ $array[$_] }{f}++, $hash{ $array[$_] }{i} = $_ for reverse
(0 .. $#array);
my $max = (sort { $hash{$b}{f} <=> $hash{$a}{f} or $hash{$a}{i}
<=> $hash{$b}{i} } keys %hash)[0];
print qq(element $max at index $hash{$max}{i} is most frequent \n)"
element x at index 0 is most frequent C:[EMAIL PROTECTED]>perl -we "use strict; my @array = qw(x CMSIF CMSIF CMSFV x
a b c d x e CMSIF);
my %hash; $hash{ $array[$_] }{f}++, $hash{ $array[$_] }{i} = $_ for (0 ..
$#array);
my $max = (sort { $hash{$b}{f} <=> $hash{$a}{f} or $hash{$a}{i}
<=> $hash{$b}{i} } keys %hash)[0];
print qq(element $max at index $hash{$max}{i} is most frequent \n)"
element x at index 9 is most frequent C:[EMAIL PROTECTED]>perl -we "use strict; my @array = qw(x CMSIF CMSIF CMSFV x
a b c d x e CMSIF);
my %hash; $hash{ $array[$_] }{f}++, $hash{ $array[$_] }{i} = $_ for (0 ..
$#array);
my $max = (sort { $hash{$b}{f} <=> $hash{$a}{f} or $hash{$b}{i}
<=> $hash{$a}{i} } keys %hash)[0];
print qq(element $max at index $hash{$max}{i} is most frequent \n)"
element CMSIF at index 11 is most frequent ------------------ end examples -----------------------
the ``$hash{ $array[$_] }{f}++, $hash{ $array[$_] }{i} = $_ for (0 ..
$#array);'' counts occurrences of
each element of array and also tracks highest/lowest index of the
element.
the ``for (0 .. $#array)'' versus ``for reverse (0 .. $#array)'' determines
whether the highest or lowest,
respectively, element index is tracked.
the ``my $max = (sort { $hash{$b}{f} <=> $hash{$a}{f} or $hash{$b}{i}
<=> $hash{$a}{i} } keys %hash)[0];''
sorts descending by frequency of occurrence and
then ascending/descending by highest/lowest (see
above) element index; then it picks the highest frequency array element
(with an array index depending
on sort order). $a <=> $b gives ascending order, $b
<=> $a gives descending order.
in general, higher frequency elements will be at lower indices after
sorting.
adjust these variations on the main theme until you get what you
want.
hth -- bill walters
|
_______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
