[ltp] perl question:

David Josephsen linux-thinkpad@www.bm-soft.com
Wed, 23 May 2001 14:47:18 -0500


dont post crap like that here...=20

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=3D0;$i<$array;$i++){
	$array2[$i]=3D0;
	for($c=3D0;$c<$i;$c=3D=3D){
		if ($array[$i]=3D=3D$array($c){
			$array2[$i]++;
		]
	}
	for($c=3D($i+1);$c<$array;$c=3D=3D){
		if ($array[$i]=3D=3D$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...=20

hope that helped.=20


>>> 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 =3D 2 4 6 3 4 8 1 9 7 5

Mode =3D 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