[ltp] perl question:

Jones, John linux-thinkpad@www.bm-soft.com
Wed, 23 May 2001 08:15:21 -0700


Dan,
Look at associative arrays.  There's a way to take each item in this array
and treat it like a name; associative arrays keep track of name value pairs,
like a hash table.  It would be something like this (I'm rusty...)

foreach $i in (@array) # I'm not sure about this construct.
{                      # What I'm trying to say is "for each element in
@array"
%assocArray($i)++;     # "add an element to assocArray and give it a value
of 1.
}                      #  if it's already there, it will be incremented by
1."

# now you have an associative array of name value pairs.
# the lvalue is the name (2,4,6,3...) and the rvalue is the count(1,2,3...)
# here would go your code to use array functions to find the highest value.

Hope this helps.
john


> -----Original Message-----
> From: ROOT, DAN [mailto:DANROO@SAFECO.com]
> Sent: Wednesday, May 23, 2001 8:03 AM
> To: 'linux-thinkpad@www.bm-soft.com'
> Subject: [ltp] perl question:
> 
> 
> I know this has little to nothing to do with Linux on 
> ThinkPads other than I
> use my ThinkPad for school, but thought I might ask anyway.  
> For my class I
> have created a random numeric array which has 10 elements.  I 
> am suppose to
> do a few things with it, but the one thing I seem to be a 
> little stumped on
> is finding the most common occurrence of an element and then 
> returning it's
> value.
> 
> Example:
> 
> @array = 2 4 6 3 4 8 1 9 7 5
> 
> Mode = 4
> 
> How do I capture that "4" is the most common element of the 
> array?  I have
> tried using grep and a loop, but I am not getting the results 
> I had hoped
> for.  Any pointers from anyone would be muchly appreciated.
> 
> Thanks!
> 
> Dan Root
> Safeco Information Systems
> danroo@safeco.com
> 206-545-3250
> 
> 
> 
> ----- The Linux ThinkPad mailing list -----
> The linux-thinkpad mailing list home page is at:
> http://www.bm-soft.com/~bm/tp_mailing.html
> 

----- The Linux ThinkPad mailing list -----
The linux-thinkpad mailing list home page is at:
http://www.bm-soft.com/~bm/tp_mailing.html