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?
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.
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.
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.
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.
There are many emergent properties. Primarily,
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.
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