<?xml version="1.0" encoding="windows-1252"?>
<node id="1008463" title="How can one call the lowest value of an array by reference?" created="2012-12-12 02:43:53" updated="2012-12-12 02:43:53">
<type id="115">
perlquestion</type>
<author id="942579">
supriyoch_2008</author>
<data>
<field name="doctext">
&lt;p&gt;Hi PerlMonks,&lt;/p&gt;
&lt;p&gt;I am interested to find out the lowest value (2) from an array 
i.e. @values=qw/5 3 2 9 2/; and to find the corresponding animal name(s) associated 
with the lowest value (2) from the array @names=qw/Cat Bat Cow Dog Rat/. I have written the following script key1.pl to find 
the lowest value, the position of the lowest value and then to call the animal name by 
position. The cmd gives the correct result for position 4(Rat) and not for 2(Cow). 
Since the lowest value 2 appears at two positions in the array, the correct result 
should be Cow (position 2) and Rat (position 4). I am at my wit's end to get the correct 
result. May I request perlmonks to provide some suggestions. I know that %hash can 
be used but use of %hash appears a little complicated to me. In fact, I tried using 
%hash but failed. Then I have tried using the given approach i.e. lowest value-&gt;position-&gt;
calling the name.&lt;/p&gt;


&lt;p&gt;Here goes the code for key1.pl&lt;/p&gt;
&lt;code&gt;#!/usr/bin/perl  
use warnings;
## To find lowest value and to call by reference:  
 @values=qw/5 3 2 12 2/;
 @names=qw/Cat Bat Cow Dog Rat/;  
# To find the lowest value from the array A: 
$min=999;  
foreach my $item (@values) {$min=$item if $min &gt; $item;}   
 print"\n\n Lowest value= $min\n"; 
# To find the position of LOWEST Value(2) in Array values:
 @array_element_pos{@values}=(0..$#values);
 $i=$array_element_pos{$min};
print"\n Positions of Lowest Value(2)= $i (should be 2 &amp; 4)\n";
# To extract corresponding animal name from Array names:  
print"\n The animal names are (should be Cow &amp; Rat): ".$names[$i]."\n"; 
exit;&lt;/code&gt;   


&lt;p&gt;I have got the following wrong results:&lt;/p&gt;
&lt;code&gt;C:\Users\x&gt;cd desktop
C:\Users\x\Desktop&gt;key1.pl

 Lowest value= 2

 Positions of Lowest Value(2)= 4 (should be 2 &amp; 4)

 The animal names are (should be Cow &amp; Rat): Rat &lt;/code&gt;

&lt;p&gt;But the correct results should look like:&lt;/p&gt;

&lt;code&gt;Lowest value= 2

Positions of Lowest Value(2)= 2  4 (should be 2 &amp; 4)

The animal names are (should be Cow &amp; Rat): Cow Rat &lt;/code&gt;

</field>
</data>
</node>
