Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hi PerlMonks,

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 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->position-> calling the name.

Here goes the code for

#!/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 > $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 & 4)\n"; # To extract corresponding animal name from Array names: print"\n The animal names are (should be Cow & Rat): ".$names[$i]."\n" +; exit;

I have got the following wrong results:

C:\Users\x>cd desktop C:\Users\x\Desktop> Lowest value= 2 Positions of Lowest Value(2)= 4 (should be 2 & 4) The animal names are (should be Cow & Rat): Rat

But the correct results should look like:

Lowest value= 2 Positions of Lowest Value(2)= 2 4 (should be 2 & 4) The animal names are (should be Cow & Rat): Cow Rat

In reply to How can one call the lowest value of an array by reference? by supriyoch_2008

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (5)
    As of 2018-06-23 10:51 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (125 votes). Check out past polls.