Perl hash keys and values are scalars, and nothing but scalars
One of the confusions and pitfalls of using perl hash is incorrectly retrieve hash keys and values as arrays or hashes. This is incorrect. Perl hash keys and values are always scalars. Then how can one build complicated data structures using perl hash. The answer lies in the simple fact that references are regarded as scalars in perl. So instead of storing arrays or hashes as hash keys and values. One stores references to arrays or hashes as keys and values of a hash. An example will make the above points clearer:
# retrieve keys and values from ihash, then split each value array using key as separator.
foreach (my $key, my $values) (%ihash) {
my @result = split($key, @$values);
}
# retrieve keys and values from ihash, then split each value array using key as separator.
foreach (my $key, my $values) (%ihash) {
my @result = split($key, @$values);
}
<< Home