|
Posted by Tad J McClellan on April 20, 2008, 9:33 am
Please log in for more thread options >
>>>Whis is difference between map and for-each?
>>
>> They have very little in comon except that both loop over the elements
>> of a list. map() is a function, foreach is a statement modifier or a
>> compound (loop) statement.
>>
>> While sometimes map() can be used to achive similar results as a
>> foreach, usually that's not a good idea because you create a return
>> value only to throw it away.
>> Vice-versa you can use map() to return a completely different list than
>> its argument while modifying the list of a foreach loop is strongly
>> discouraged and may lead to very unexpected results.
>> Also closures are more natural with map().
>>
>>>Why the map function is fast than for-each? <== is it for all case?
>>
>> It is? That would surprise me, but I haven't run any benchmarks.
>
> I think of foreach as a loop and map as a function like this:
> map ($_->[$subject_offset].' from '.$_->[$from_offset], @xover)
You didn't show where the return value from map() is going, I'll assume:
@list = map ($_->[$subject_offset]...
> They don't seem like comparables to me.
@list = ();
foreach ( @xover ) { # untested
push @list, $_->[$subject_offset].' from '.$_->[$from_offset];
}
The contents of @list are the same either way.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"
|