Question: How do I reference perl hash? How do I deference perl hash? Can you explain it with a simple example?
Answer: In our previous article we discussed about Perl array reference. Similar to the array, Perl hash can also be referenced by placing the ‘\’ character in front of the hash. The general form of referencing a hash is shown below.
%author = ( 'name' => "Harsha", 'designation' => "Manager" ); $hash_ref = \%author;
This can be de-referenced to access the values as shown below.
$name = $ { $hash_ref} { name };
Access all keys from the hash as shown below.
my @keys = keys % { $hash_ref };
The above code snippet is same as the following.
my @keys = keys %author;
If the reference is a simple scalar, then the braces can be eliminated.
my @keys = keys %$hash_ref;
When we need to reference the particular element, we can use -> operator.
my $name = $hash_ref->{name};
Make reference to an anonymous Perl hash as shown below.
my $hash_ref = {
'name' => "Harsha",
'designation' => "Manager"
};
De-Referencing this hash is same as we did for the above example (%author).
$name = $ { $hash_ref} { name };
Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.Get the Linux Sysadmin Course Now!
If you enjoyed this article, you might also like..
|
|
|
|






My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 2 comments… read them below or add one }
how can i derefere hash reference to array?
Hi.
By “hash reference to an array”, do you mean this?
my %hash = (
‘mary’ => ['female', 18, 'student'],
);
my $href = \%hash;
print ${$href}{‘mary’}->[2];
Hope this helps.