Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

Here goes the code for key1.pl

#!/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>key1.pl 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

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (11)
    As of 2014-08-01 14:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Who would be the most fun to work for?















      Results (25 votes), past polls