In Perl, how do I create a hash whose keys come from a given array?

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

It’s not as short as the “@hash{@array} = …” solutions, but those ones require the hash and array to already be defined somewhere else, whereas this one can take an anonymous array and return an anonymous hash.

What this does is take each element in the array and pair it up with a “1”. When this list of (key, 1, key, 1, key 1) pairs get assigned to a hash, the odd-numbered ones become the hash’s keys, and the even-numbered ones become the respective values.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)