This...: "@nnnumber_of_file_low" interpolates an array into a string. It's similar to: join q{ }, @nnnumber_of_file_low;. So you aren't actually putting multiple values into the hash elements. You're putting a single scalar into each hash element.

Then you say my( $value1, $value2 ) = $nn_file{$key};, but since $nn_file{$key} only contains a single scalar value, what you're really saying is something similar to my ($value1, $value2) = ( $nn_file{$key}, undef );.

So when you print "key is $key value is $value1 $value2";, you're really saying something like: print "key is ((some key)) value is ((some value)) ((undef))";. Perl complains when you interpolate an undefined value into a string, because that's usually a mistake, as it is here. It's only by virtue of Perl's DWIMery that undef stringifies to an empty string. And it's warnings that warns you about it.

Hash elements are scalar values, as are array elements. If you want to store more than one thing in a hash element, you have to store a reference to another container. Recommended reading: perlreftut, perldsc, perllol, and perlref.

As an example of what you probably intend to do:

my %hash; $hash{some_key} = [ $this, $that ]; # [ ... ] creates an anonymous ar +ray ref. $hash{other_key} = [ $the, $other ]; # and that is assigned to the has +h. foreach my $key ( keys %hash ) { print "key: $key, values: $hash{$key}[0], $hash{$key}[1]\n"; }

Dave


In reply to Re: assign multiple values of a key in hash to multiple variable by davido
in thread assign multiple values of a key in hash to multiple variable by rahulruns

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!
  • 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, details, 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, summary, 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.