Printing hash value in Perl

12,644

Solution 1

I can't answer completely, because it depends entirely on what's in $HashVariable.

The easiest way to tell what's in there is:

use Data::Dumper;
print Dumper $HashVariable;

Assuming this is a hash reference - which it would be, if print $HashVariable gives HASH(0xdeadbeef) as an output.

So this should work:

#!/usr/bin/env perl

use strict;
use warnings;

my $HashVariable = { somekey => 'somevalue' }; 

foreach my $key ( keys %$HashVariable ) { 
   print $key, " => ", $HashVariable->{$key},"\n";
}

The only mistake you're making is that $HashVariable{$key} won't work - you need to dereference, because as it stands it refers to %HashVariable not $HashVariable which are two completely different things.

Otherwise - if it's not entering the loop - it may mean that keys %$HashVariable isn't returning anything. Which is why that Dumper test would be useful - is there any chance you're either not populating it correctly, or you're writing to %HashVariable instead.

E.g.:

my %HashVariable;
$HashVariable{'test'} = "foo";

Solution 2

There's an obvious problem here, but it wouldn't cause the behaviour that you are seeing.

You think that you have a hash reference in $HashVariable and that sounds correct given the HASH(0xd1007d0) output that you see when you print it.

But setting up a hash reference and running your code, gives slightly strange results:

my $HashVariable = {
  foo => 1,
  bar => 2,
  baz => 3,
};

foreach my $var(keys %{$HashVariable}){
    print"In the loop \n";
    print"$var and $HashVariable{$var}\n";
}

The output I get is:

In the loop 
baz and 
In the loop 
bar and 
In the loop 
foo and 

Notice that the values aren't being printed out. That's because of the problem I mentioned above. Adding use strict to the program (which you should always do) tells us what the problem is.

Global symbol "%HashVariable" requires explicit package name (did you forget to declare "my %HashVariable"?) at hash line 14.
Execution of hash aborted due to compilation errors.

You are using $HashVariable{$var} to look up a key in your hash. That would be correct if you had a hash called %HashVariable, but you don't - you have a hash reference called $HashVariable (note the $ instead of %). To look up a key from a hash reference, you need to use a dereferencing arrow - $HashVariable->{$var}.

Fixing that, your program works as expected.

use strict;
use warnings;

my $HashVariable = {
  foo => 1,
  bar => 2,
  baz => 3,
};

foreach my $var(keys %{$HashVariable}){
    print"In the loop \n";
    print"$var and $HashVariable->{$var}\n";
}

And I see:

In the loop 
bar and 2
In the loop 
foo and 1
In the loop 
baz and 3

The only way that you could get the results you describe (the HASH(0xd1007d0) output but no iterations of the loop) is if you have a hash reference but the hash has no keys.

So (as I said in a comment) we need to see how your hash reference is created.

Share:
12,644
emma
Author by

emma

Updated on June 04, 2022

Comments

  • emma
    emma about 2 years

    When I print a variable, I am getting a HASH(0xd1007d0) value. I need to print the values of all the keys and values. However, I am unable to as the control does not enter the loop.

    foreach my $var(keys %{$HashVariable}){
        print"In the loop \n";
        print"$var and $HashVariable{$var}\n";
    
    }
    

    But the control is not even entering the loop. I am new to perl.

    • ssr1012
      ssr1012 about 7 years
      you are getting referencing the hashes in scalar?
    • zdim
      zdim about 7 years
      If the variable $HashVariable is indeed a hashref the only possibility is that it is empty, without any keys. Otherwise the code would get in the loop.
    • ssr1012
      ssr1012 about 7 years
      could you please share the hash where is getting from
    • AbhiNickz
      AbhiNickz about 7 years
      Show us the output of print Dumper($HashVariable);
    • Dave Cross
      Dave Cross about 7 years
      @emma: We really need to see how the variable $HashVariable is constructed. Without that all our answers are guesses.