Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Perl hashes always contain values?

Tags:

hash

perl

I had an earlier question that received the following response from the noted Perl expert, Perl author and Perl trainer brian d foy:

[If] you're looking for a fixed sequence of characters at the end of each filename. You want to know if that fixed sequence is in a list of sequences that interest you. Store all the extensions in a hash and look in that hash:
    my( $extension ) = $filename =~ m/\.([^.]+)$/;
    if( exists $hash{$extension} ) { ... }
You don't need to build up a regular expression, and you don't need to go through several possible regex alternations to check every extension you have to examine.

Thanks for the advice brian.

What I now want to know is what is the best practice in a case like the above. Should one only define the keys, which is all I need to achieve what's described above, or should one always define a value as well?

like image 878
Dr. Faust Avatar asked May 27 '09 19:05

Dr. Faust


People also ask

How do I create an empty hash in Perl?

You need to use the \ operator to take a reference to a plural data type (array or hash) before you can store it into a single slot of either. But in the example code given, if referenced, each would be the same hash.

Is hash empty Perl?

From perldoc perldata: If you evaluate a hash in scalar context, it returns false if the hash is empty. If there are any key/value pairs, it returns true; more precisely, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash.

How do hashes work in Perl?

A Perl hash is defined by key-value pairs. Perl stores elements of a hash in such an optimal way that you can look up its values based on keys very fast. With the array, you use indices to access its elements. However, you must use descriptive keys to access hash's element.

How do I initialize a hash in Perl?

There are two ways to initialize a hash variable. One is using => which is called the fat arrow or fat comma. The second one is to put the key/value pairs in double quotes(“”) separated by a comma(,).


1 Answers

It's usually preferable to set a defined value for every key. The idiomatic value (when you don't care about the value) is 1.

my %hash = map { $_ => 1 } @array;

Doing it this way makes the code the uses the hash slightly simpler because you can use $hash{key} as a Boolean value. If the value can be undefined you need to use the more verbose exists $hash{key} instead.

That said, there are situations where a value of undef is desirable. For example: imagine that you're parsing C header files to extract preprocessor symbols. It would be logical to store these in a hash of name => value pairs.

#define FOO 1
#define BAR

In Perl, this would map to:

my %symbols = ( FOO => 1, BAR => undef);

In C a #define defines a symbol, not a value -- "defined" in C is mapped to "exists" in Perl.

like image 117
Michael Carman Avatar answered Oct 19 '22 03:10

Michael Carman