Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl access value from Hash Reference

In my Perl code, I ended up having a hash reference like below. I would like to access an individual element from it. I tried multiple ways, but I was not able to fetch it.

#!/usr/bin/perl
#use strict;
use Data::Dumper;
my %h={'one'=>1,'two'=>2};
print Dumper($h{'one'});

Output

$VAR1 = undef;
like image 582
Vinoth Karthick Avatar asked Dec 23 '22 16:12

Vinoth Karthick


1 Answers

Use parentheses to construct your hash, not braces:

use strict;
use warnings;
use Data::Dumper;

my %h = ('one'=>1, 'two'=>2);
print Dumper($h{'one'});

Braces are used to construct a hash reference.

Also, add use warnings;, which would have generated a message that there was a problem with your code.


Or, if you really wanted a hashref:

my $h = {'one'=>1, 'two'=>2};
print "$h->{one}\n";
like image 115
toolic Avatar answered Jan 02 '23 16:01

toolic