Meditation, The Art of Exploitation

Thinking? At last I have discovered it--thought; this alone is inseparable from me. I am, I exist--that is certain. But for how long? For as long as I am thinking. For it could be, that were I totally to cease from thinking, I should totally cease to exist....I am, then, in the strict sense only a thing that thinks.

Wednesday, October 04, 2006

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);
}