On 3/9/11 Wed  Mar 9, 2011  9:22 AM, "Brian F. Yulga"
<byu...@langly.dyndns.org> scribbled:

> Uri and Jim have hit upon one of my major stumbling blocks with learning
> Perl.  There seems to be a difference of opinion on the proper times to
> use hashes vs. arrays/lists...and how best to use them.  For those that
> have heard of this book, I'm currently digesting this piece of information:
> 
> Effective Perl Programming, 2nd Ed., Item 9: "Know the difference
> between lists and arrays."
> 
> I have this suspicion that it relates to how C/C++ programmers write C
> programs in Perl, rather than Perl programs.  For instance, someone in
> the "C mindset" might be tempted to write the above 'grep' as (following
> code is a bad idea, I know...):
> 
> foreach ( @word_list ) {
>      if ( /^$temp_word$/i ) {
>          push( @all_combinations, ( $_ ));
>      }
> }

That is pretty much what the grep function is doing. It has to iterate over
the entire array and evaluate its code block for each element and form a
list of the array elements that evaluate to true. There is no way to
eliminate the requirement to scan the entire array.

> 
> I understand that this is very inefficient in Perl, and (please correct
> me if I'm wrong) 

You are wrong.

> that Perl is optimized to search and manipulate hashes
> and lists as a whole, rather than executing code over each item
> one-at-a-time.  So, I get the idea that I should treat a list of items
> as a LIST (as a whole), rather than the conventional thinking of
> enumerating each item of a list into an array.  Please tell me, gurus,
> if I'm on the right track!  ;-)

You are not.

> 
> So, my question becomes, why is hash manipulation more efficient than
> list manipulation??

Because to find an element in a hash does not require iterating over the
entire hash. Perl can find a (key,value) pair by evaluating the hash of the
key, then going right to where the value is stored. It is almost like
finding the element of an array knowing the index value, rather than having
to scan the entire array looking for a certain value.

> Uri states that hashes are "better for for token
> lookups than scanning arrays".  But each key in the hash has an unused
> associated value (1) in this case, which increases the amount of data
> stored in memory.  Maybe that's inconsequential.

It is.

> But, if we "grep a
> list" (as Jim suggests) instead of "scan an array", shouldn't the list
> manipulation be more efficient (or at least equivalently efficient) to a
> hash lookup?

No, grep has to scan the entire array, one element at a time. Using grep
instead of a for loop might be more efficient, because of the internal
mechanisms of Perl, but the time required is still proportional to the
number of elements in the array. There is no way around that.

Not much can be done with the list "as a whole". You can take a reference to
it and find out how many elements, but anything else productive will almost
surely involve scanning the array and accessing the elements, one at a time.





-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to