Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

assign multiple values of a key in hash to multiple variable

by rahulruns (Beadle)
on Aug 21, 2013 at 05:08 UTC ( #1050281=perlquestion: print w/ replies, xml ) Need Help??
rahulruns has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to assign the mutiple values of a key in a hash to multiple variables but getting "Use of uninitialized value $value2 in concatenation (.) or string at checking.pl line 39." error though the values of the keys are assigned to the variables. How do I eliminate these error messages?

#!/usr/bin/perl use strict; use Term::ANSIColor; use warnings; use Crypt::Random qw( makerandom ); #--------------------------------------------------------------------- +-------- #RANDOM NUMBER GENERATION FOR NNBENCH #--------------------------------------------------------------------- +-------- #print color 'bold blue'; print "NNBENCH TEST TO START\n\n"; my $nn_map = makerandom ( Size => 5, Strength => 1 ); my $nn_reduce = makerandom ( Size => 4, Strength => 1 ); my $nnblock_size_low = makerandom ( Size => 7, Strength => 1 ); my $nnblock_size_medium = makerandom ( Size => 10, Strength => 1 ); my $nnblock_size_huge = makerandom ( Size => 11, Strength => 1 ); my $nnbyte_size_low = makerandom ( Size => 7, Strength => 1 ); my $nnbyte_size_medium = makerandom ( Size => 9, Strength => 1 ); my $nnbyte_size_huge = makerandom ( Size => 14, Strength => 1 ); my $nnfile_number_low = makerandom ( Size => 7, Strength => 1 ); my $nnfile_number_medium = makerandom ( Size => 9, Strength => 1 ); my $nnfile_number_huge = makerandom ( Size => 14, Strength => 1 ); my @nnnumber_of_file_low = ("$nnblock_size_low", "$nnbyte_size_low"); my @nnnumber_of_file_medium = ("$nnblock_size_medium", "$nnbyte_size_m +edium"); my @nnnumber_of_file_huge = ("$nnblock_size_huge", "$nnbyte_size_huge" +); my %nn_file = ($nnfile_number_low => "@nnnumber_of_file_low" , $nnfile +_number_medium => "@nnnumber_of_file_medium", $nnfile_number_huge => +"@nnnumber_of_file_huge");; foreach my $key ( keys %nn_file ) { my ($value1,$value2) = $nn_file{$key}; print "key is $key value is $value1 $value2"; ERROR #perl checking.pl NNBENCH TEST TO START Use of uninitialized value $value2 in concatenation (.) or string at c +hecking.pl line 39. Use of uninitialized value $value2 in concatenation (.) or string at c +hecking.pl line 39. Use of uninitialized value $value2 in concatenation (.) or string at c +hecking.pl line 39. key is 120 value is 93 84 key is 8364 value is 1435 8883 key is 405 +value is 647 313

Comment on assign multiple values of a key in hash to multiple variable
Download Code
Re: assign multiple values of a key in hash to multiple variable
by davido (Archbishop) on Aug 21, 2013 at 05:26 UTC

    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

      Thank You Dave for the explanation. I did sort out the solution by introducing split and thus resolving the undef issue

      my ($value1,$value2) = split (' ', $nn_file{$key});

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1050281]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-12-27 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls