Jim Gibson wrote:
On 3/9/11 Wed Mar 9, 2011 9:22 AM, "Brian F. Yulga"
<byu...@langly.dyndns.org> scribbled:
>
> 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.
Ah, my (poor) assumption was that 'grep' was more magical in its
internal mechanisms for searching. Since it still acts on each member
of the list, the rest of my argument is defunct.
>
> I understand that this is very inefficient in Perl, and (please
> correct me if I'm wrong)
You are wrong.
> each item of a list into an array. Please tell me, gurus, if I'm
> on the right track! ;-)
You are not.
Ouch. Well, I'm here to learn.
>
> 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.
so... key converts to hash in one step, then jumps directly to that
position (if hash for that key is defined). And if you're only looking
up the existence of the key in the table, no additional resources are
used to read the value from memory. I can see how that's way more
efficient than scanning all array indexes and reading each value.
Especially for large lists. But, there must be a slight trade-off...
the processing required to initialize the hash table with it's keys and
values is probably more intensive than defining an array with its
respective values? Unless, internally, Perl stores arrays as hashes,
with the indexes as the keys.
> 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.
Thanks much for the explanation...now I understand Uri's comments! and
I can appreciate that hashes are more powerful and flexible than I
originally gave them credit for (I always thought of hashes as just
fancy arrays with strings instead of numbers for indexes).
It also makes me wonder if the time spent learning linked lists in C
programming class would have been better spent learning useful
programming theory with Perl...
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/