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;
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With