What is the difference between a hash and a hash reference in Perl?

I would like to understand hashes in Perl correctly. I had to use Perl intermittently for some time, and in most cases when I need it, it is mainly related to text processing.

And every time I have to deal with hashes, it got messed up. I find the syntax very cryptic for hashes

A good explanation of hashes and hash links, their differences when they are needed, etc. will be greatly appreciated.

+50
perl language-features hash
Nov 30 '09 at 1:02
source share
4 answers

A simple hash is close to an array. Their initializations even look similar. Array first:

@last_name = ( "Ward", "Cleaver", "Fred", "Flintstone", "Archie", "Bunker" ); 

Now we will present the same information with a hash (aka associative array):

 %last_name = ( "Ward", "Cleaver", "Fred", "Flintstone", "Archie", "Bunker" ); 

Although they have the same name, the array @last_name and the hash %last_name completely independent.

With an array, if we want to know the name of Archie, we need to perform a linear search:

 my $lname; for (my $i = 0; $i < @last_name; $i += 2) { $lname = $last_name[$i+1] if $last_name[$i] eq "Archie"; } print "Archie $lname\n"; 

With a hash, it is much more direct syntactically:

 print "Archie $last_name{Archie}\n"; 

Suppose we want to present information with only a slightly richer structure:

  • Cleaver (last name)
    • Ward (name)
    • June (spouse's name)
  • gravel
    • Fred
    • Wilma
  • Bunker
    • Archie
    • Edith

Before the links appeared, flat key hashes were the best we could do, but the links allow

 my %personal_info = ( "Cleaver", { "FIRST", "Ward", "SPOUSE", "June", }, "Flintstone", { "FIRST", "Fred", "SPOUSE", "Wilma", }, "Bunker", { "FIRST", "Archie", "SPOUSE", "Edith", }, ); 

Internally, the keys and values โ€‹โ€‹of %personal_info are all scalars, but values โ€‹โ€‹are a special kind of scalar links: hash links created using {} . Links allow us to model "multidimensional" hashes. For example, we can get to Wilma through

 $personal_info{Flintstone}->{SPOUSE} 

Note that Perl allows us to omit the arrows between the indices, so the above is equivalent

 $personal_info{Flintstone}{SPOUSE} 

This is a lot of typing if you want to know more about Fred, so you can get the link as a kind of cursor:

 $fred = $personal_info{Flintstone}; print "Fred wife is $fred->{SPOUSE}\n"; 

Because the $fred in the snippet above is hashref, an arrow is needed. If you leave this, but with smart inclusion use strict to help you catch such errors, the compiler will complain:

 Global symbol "%fred" requires explicit package name at ... 

Perl references are similar to pointers to C and C ++, but they can never be null. Pointers to C and C ++ require dereferencing, as well as references to Perl.

The parameters of the C and C ++ functions have pass-by-value semantics: they are just copies, so modifications do not return to the caller. If you want to see the changes, you need to pass a pointer. You can get this effect with links to Perl:

 sub add_barney { my($personal_info) = @_; $personal_info->{Rubble} = { FIRST => "Barney", SPOUSE => "Betty", }; } add_barney \%personal_info; 

Without a backslash, add_barney would get a copy that was thrown as soon as sub returned.

Also note the use of the bold point ( => ) above. It automatically prints the line to the left and makes hash initialization less syntactically noisy.

+76
Nov 30 '09 at 2:38
source share

The following shows how you can use the hash and hash link:

 my %hash = ( toy => 'aeroplane', colour => 'blue', ); print "I have an ", $hash{toy}, " which is coloured ", $hash{colour}, "\n"; my $hashref = \%hash; print "I have an ", $hashref->{toy}, " which is coloured ", $hashref->{colour}, "\n"; 

Also see perldoc perldsc .

+15
Nov 30 '09 at 2:34
source share

A hash is a basic data type in Perl. It uses keys to access its contents.

A hash ref is an abbreviation for hash reference. Links of scalars, i.e. simple values. this is a scalar value that essentially contains a pointer to the actual hash itself.

Link: the difference between hash and hash ref in perl - Ubuntu Forums

The difference also lies in the deletion syntax. Like C, perl works like a hash:

 delete $hash{$key}; 

and for hash links

 delete $hash_ref->{$key}; 

Perl Hash Howto is a great resource for understanding hash and hash with hash links

There is another link here that contains additional information about perl and links .

+10
Nov 30 '09 at 1:07
source share

See perldoc perlreftut , which is also available on your own computer command line.

A link is a scalar value that refers to the entire array or the entire hash (or anything else). Names are one type of link you are already familiar with. Think of the President of the United States: a dirty, uncomfortable bag of blood and bones. But to talk about it or to present it in a computer program, all you need is a simple and convenient scalar line "Barack Obama."

References in Perl are like array and hash names. These are Perl private, internal names, so you can be sure that they are unambiguous. Unlike Barack Obama, a link refers to only one thing, and you always know what it refers to. If you have an array reference, you can restore the entire array from it. If you have a hash reference, you can restore the entire hash. But this link is still a simple and compact scalar value.

+6
Nov 30 '09 at 1:25
source share



All Articles