Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
In addition to kennethk and wind valuable input, I have the following comments about your code...
  • LOOPING: in arrays, a scenario for looping is when we don't have prior information about what index position an array element resides in for us to readily reach it, on the other hand in hashes, (associative arrays is another name), the keys are the indices, so you basically were retrieving a certain hash value based on whether the index for that value exists within the hash or not, so in this context looping wasn't a necessary thing at all since each key is associated with a value..
  • CONTEXT: The context where you used the functions 'keys' and 'values' is a scalar context, so it returns to you only the number of keys and the number of values respectively and assigns that to $key and $val , if you assigned the outcome of these functions to arrays you'd have returned the actual hash keys and hash values themselves. Read Context tutorial from the Tutorials section
  • Finally; from your code, "$key1 is already there, and its family name is $family_name{$key1} ", replace 'its' by 'her'
#Consider the output of the following my %family_name = ( "Alis" => "Williams", "Sara" => "McAwesome", "Serena" => "Anderson", ); my $key = keys %family_name; print $key; print "\n"; my $val = values %family_name; print $val; print "\n"; my @keys = keys %family_name; print "@keys\n"; my @values = values %family_name; print "@values\n";
So, availing of the context property in Perl we can achieve what you sought in a TIMTOWTDI yet somewhat convoluted manner. Just as a proof of concept and maybe a review of some of the things you have studies so far consider the following code... Comments are duly provided
use strict; use warnings; #The hash is populated as in the OP code above #The user is prompted to enter a name my @keys = keys %family_name; my @values = values %family_name; #The arrays members are in the same order the hash #is stored in the memory and not necessarily the #order in which the hash has been populated. my $index; for(my $i = 0; $i<=$#keys; $i++){ #looping through @keys if($name eq $keys[$i]){ $index = $i; #to retain the iterator value to $index retHashVal($name, $index); } } sub retHashVal{ my ($n,$i) = @_; print "$n is already there and her family name is $values[$i]\ +n"; }


Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

In reply to Re: Comparing Hash's key to an <STDIN> and print it's corresponding value by biohisham
in thread Comparing Hash's key to an <STDIN> and print it's corresponding value by xxArwaxx

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-03-28 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found