Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it better to use an array instead of a hash in Perl?

Tags:

arrays

hash

perl

Say you have an array @a = qw/ a b c d/;

and a hash %a = ('a' => 1, 'b' => 1, 'c' => 1, 'd' => 1);

Is there any situation where creating the array version is better than creating the hash (other than when you have to iterate over all the values as in something like

for (@a){
    ....

in which case you would have to use keys %a if you went with the hash)? Because testing whether a specific value is in a hash is always more efficient than doing so in an array, correct?

like image 328
Tyler Avatar asked Aug 14 '14 17:08

Tyler


People also ask

What is an example of hash in Perl?

Hash of Arrays in Perl Elements of hash can be anything, including references to array. For example what if you have a bunch of people and each person has a list of scores. Another interesting example would be a bunch of people each person belonging to 1 or more groups.

Can an array do anything a hash table can do?

To simulate an array via a hash table, one can simply set the keys of the hash table to be the indices of the array, and set the value of each key to be the value of the array at that index. In this way, it would seem that anything an array can do, a hash table can do, as well.

What are hashes in programming?

But let's move on to hashes. Many languages use structures like Perl hashes, which are really just associative arrays. Some languages (Java, JavaScript, Go, and some others) call them maps; others (including PostScript) call them dictionaries, and in PHP and MUMPS, all arrays are really associative arrays that behave somewhat like Perl hashes.

How to get the keys of a hash as an array?

Perl gives you a handy way to get the keys of a hash as an array: Hashes, unlike arrays, are not ordered, so if you want things in some order, you'll need to implement that. A sort on the keys is a common way of doing that.


2 Answers

    • Arrays are indexed by numbers.
    • Hashes are keyed by strings.
    • All indexes up to the highest index exist in an array.
    • Hashes are sparsely indexed. (e.g. "a" and "c" can exist without "b".)

There are many emergent properties. Primarily,

    • Arrays can be used to store ordered lists.
    • It would be ugly an inefficient to use hashes that way.
    • It's not possible to delete an element from an array unless it's the highest indexed element.
    • You can delete from an ordered list implemented using an array, though it is inefficient to remove elements other than the first or last.
    • It's possible to delete an element from a hash, and it's efficient.
like image 68
ikegami Avatar answered Oct 07 '22 12:10

ikegami


Arrays are ordered lists of values. They can contain duplicate values.

@array = qw(a b c a);

Hashes are a mapping between a key (which must be unique) and a value (which can be duplicated). Hashes are (effectively) unordered, which means that keys come out in apparently random order rather than the order in which they are entered.

%hash = (a => 1, b => 2, c => 3);

Hashes can also be used as sets when only the key matters. Sets are unordered and contain only unique "values" (the hash's keys).

%set = (a => undef, b => undef, c => undef);

Which one to use depends on your data and algorithm. Use an array when order matters (particularly if you can't sort to derive the order) or if duplicate values are possible. Use a set (i.e. use a hash as a set) when values must be unique and don't care about order. Use a hash when uniqueness matters, order doesn't (or is easily sortable), and look-ups are based on arbitrary values rather than integers.

You can combine arrays and hashes (via references) to create arbitrarily complex data structures.

@aoa = ([1, 2, 3], [4, 5, 6]);               # array of arrays ("2D" array)
%hoh = (a => { x => 1 }, b => { x => 2 });   # hash of hashes
@aoh = ({a => 1, b => 2}, {a => 3, b => 4}); # array of hashes
%hoa = (a => [1, 2], b => [3, 4]);           # hash of arrays
...etc.
like image 45
Michael Carman Avatar answered Oct 07 '22 13:10

Michael Carman