Petr, since you went to the effort to improve the original code, here is
a minor improvement to yours. No need to resort since he wasn't
interested the relative values of everything else.
my @a = qw(CMSIF FUBAR CMSIF CMSFV CMSIF FUBAR);
my %h;
my @max=("",0);
map { $h{$_}++; @max=($_,$h{$_}) if $h{$_} > $max[1]; } @a;
print "$max[0] occurs $max[1]\n";
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Petr
Vileta
Sent: Thursday, June 01, 2006 7:36 AM
To: [email protected]
Subject: Re: Analyzing perl array to extract maximum occuring element
----- Original Message -----
From: "$Bill Luebkert" <[EMAIL PROTECTED]>
To: "sekhar kavuru" <[EMAIL PROTECTED]>
Cc: <[email protected]>
Sent: Thursday, June 01, 2006 3:59 AM
Subject: Re: Analyzing perl array to extract maximum occuring element
> sekhar kavuru wrote:
>
>> 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
>
> Lots of ways to do it.
> I'd probably just map it into a hash increasing the count by one
> for each occurence - then sort the hash by value.
>
> my @a = qw(CMSIF FUBAR CMSIF CMSFV CMSIF FUBAR);
> my %h;
>
> map { $h{$_}++ } @a;
> my @b = sort { $h{$a} cmp $h{$b} } keys %h;
> print "$b[0]\n";
>
Excelent Bill ! But could be improve like this:
my @a = qw(CMSIF FUBAR CMSIF CMSFV CMSIF FUBAR);
my %h;
my $max=0;
map { $h{$_}++; $max=$h{$_} if $h{$_} > $max; } @a;
my @b = sort { $h{$a} cmp $h{$b} } keys %h;
print "$b[0] occurs $max\n";
Petr Vileta, Czech republic
(My server reject all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs