hi sekar --  
 
In a message dated 5/31/2006 10:46:56 P.M. Eastern Standard Time, Williamawalters writes:
 
> 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
 
on second thought, here's a version that preserves complete information on both element
frequency and indices.  
 
 
C:\perl>perl -we "use strict; my %hash; push @{$hash{$ARGV[$_]}}, $_ for (0 .. $#ARGV);
my @freq = sort { @{ $hash{$b} } <=> @{ $hash{$a} } or $a cmp $b } keys %hash;
local $\" = ', '; printf qq(%3d %-3s (@{$hash{$_}}) \n), scalar(@{ $hash{$_} }), $_ for @freq"
a x ba c foo x e d e d fe bar x g
  3 x   (1, 5, 12)
  2 d   (7, 9)
  2 e   (6, 8)
  1 a   (0)
  1 ba  (2)
  1 bar (11)
  1 c   (3)
  1 fe  (10)
  1 foo (4)
  1 g   (13)
 
 
hth -- bill walters  
 
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to