Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl - Why does looping over a hash by its keys and then printing each value result in a uninitialized warning?

Tags:

foreach

hash

perl

Whenever I loop over a hash by its keys, and then printing each value, I get an "use of of uninitialized value in concatenation (.) or string..." warning. Even though the hash is clearly initialized up front. The output I want is printed, but I'd still like to know why this results in a warning, especially as accessing a value directly (outside of a loop) works without warnings.

#!/usr/bin/perl
use warnings;
use strict;

my %fruit = ();
%fruit = ('Apple' => 'Green', 'Strawberry' => 'Red', 'Mango' => 'Yellow');

#works
print  "An apple is $fruit{Apple} \n";

#gives warnings
foreach my $key (%fruit)
{
  print "The color of $key is $fruit{$key} \n";
}

#also gives warnings
foreach my $key (%fruit)
{
    my $value = $fruit{$key};
    print "$value \n";
}

Consider the above code. I guess perl sees a difference between the first print and the second print. But why? Why is there a difference between retrieving the value of a hash outside of loop and retrieving the value of a has inside of a loop?

Thanks!

like image 942
Gx1sptDTDa Avatar asked Jan 11 '13 09:01

Gx1sptDTDa


People also ask

How do you maintain a hash order in Perl?

Instead of just having the "seen" type of hash, it can store both the count and order noticed. Basically, instead of $count{$line} having the number of times seen, $count{$line}{count} is the times seen and $count{$line}{order} is the order in which it was seen.

How get key from hash value in Perl?

Extracting Keys and Values from a Hash variable The list of all the keys from a hash is provided by the keys function, in the syntax: keys %hashname . The list of all the values from a hash is provided by the values function, in the syntax: values %hashname . Both the keys and values function return an array.

What does keys do in Perl?

keys() function in Perl returns all the keys of the HASH as a list. Order of elements in the List need not to be same always, but, it matches to the order returned by values and each function. Return: For scalar context, it returns the number of keys in the hash whereas for List context it returns a list of keys.

What is hash key in Perl?

A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name preceded by a "$" sign and followed by the "key" associated with the value in curly brackets..


2 Answers

Using a hash in list context yields both keys and values. Therefore the line foreach my $key (%fruit) iterates over key, value, key, value...

What you need is foreach my $key (keys %fruit).

like image 171
Moritz Bunkus Avatar answered Nov 12 '22 17:11

Moritz Bunkus


It should be foreach my $key ( keys %fruits ). I think this is what you actually want to do.

like image 34
slayedbylucifer Avatar answered Nov 12 '22 19:11

slayedbylucifer