[ltp] perl question:

ROOT, DAN linux-thinkpad@www.bm-soft.com
Thu, 24 May 2001 09:29:34 -0700


Thanks David, I have been getting beat on pretty hard for posting this
question to the list.

I will know better from now on.

;^)

Dan

-----Original Message-----
From: David Josephsen [mailto:djosephs@vha.com]
Sent: Wednesday, May 23, 2001 12:47 PM
To: linux-thinkpad@www.bm-soft.com
Subject: Re: [ltp] perl question:


dont post crap like that here... 

That being said,  Im sure there are better ways of doing this.. but  the way
that becomes obious to me is  to create a new a rray that contians the
number of references to each element in the old array.. then check to see
which is the biggest number in the second array... uh, yeah... here..

for($i=0;$i<$array;$i++){
	$array2[$i]=0;
	for($c=0;$c<$i;$c==){
		if ($array[$i]==$array($c){
			$array2[$i]++;
		]
	}
	for($c=($i+1);$c<$array;$c==){
		if ($array[$i]==$array($c){
			$array2[$i]++;
		}
	}
}
There, now you should have a second array , called array2 full of 0's execpt
for the elements of array which are repeated. For example if array[2] is
repeated once,  ie the same number exists in array[4], there will be a "1"
in array2[2], and array2[4]. then all you need to do is a bunch of if-else's
to figure out which digit in array2 is the highest, and return that number.
remember there are two, but it doesn't really matter,  return either and it
will be the same number. If you want to be smart about it you can do some
checking later to see if they are duplicates and remove them. or return them
all... 

hope that helped. 


>>> DANROO@SAFECO.com 05/23/01 09:58 AM >>>
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, bu 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


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