[Israel.pm] Perl Advocacy

Offer Kaye offer.kaye at gmail.com
Wed Dec 29 12:24:17 PST 2004


On Wed, 29 Dec 2004 19:22:56 +0200, david istermann wrote:
> 
> I know that. That's the internal implementation point of view. But
> from the point of view of the end-user (that is, the Perl programmer)
> these two data types provide almost the same functionality.

No they don't. If you think that, you don't understand the different
between the two.
For example, the @INC array stores the directories where Perl searches
for included libraries. There is *no way* to get to a particular path
directly - you have to go over each item and see if it matches what
you are searching for. Now compare this with the %ENV hash, which
gives you access to the environment variables. There is *no need* go
over the entire hash, to find a variable called 'HOME' - you simply
write $ENV{'HOME'} .In fact, it doesn't make sense, usually, to go
over each item of the hash.

> Why 'each' is restricted to hashes, and 'foreach' to arrays only? I
> think I should be able to do both:

There's a famous quote by Larry Wall:'
"Doing linear scans over an associative array is like trying to club
someone to death with a loaded Uzi."

Hashes *are not* arrays, and trying to use them as such simply means,
to me at least, that you've probably selected the wrong data
structure.
If your problem requires iterating over each and every element of the
data, then using a hash, IMHO, is just *plain wrong*.

Just my 2 cents worth - I may be missing something fundamental here,
because I expected someone to bring up this point a lot sooner...

-- 
Offer Kaye



More information about the Perl mailing list