Petr Vileta <> wrote:
> ----- 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";

But surely $max ends up with the value that is already in $h{$b[0]}.

HTH

-- 
Brian Raven 


=================================
Atos Euronext Market Solutions Disclaimer
=================================
The information contained in this e-mail is confidential and solely for the 
intended addressee(s). Unauthorised reproduction, disclosure, modification, 
and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message do not 
necessarily reflect those of Atos Euronext Market Solutions.

L'information contenue dans cet e-mail est confidentielle et uniquement 
destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. 
Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail 
vous parvient par erreur, nous vous prions de bien vouloir prevenir 
l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre 
systeme. Le contenu de ce message electronique ne represente pas necessairement 
la position ou le point de vue d'Atos Euronext Market Solutions.


_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to