|
Prev: FAQ 4.44 How do I test whether two arrays or hashes are equal?
Next: FAQ 5.3 How do I count the number of lines in a file?
From: mynews on 18 Apr 2008 23:30 Whis is difference between map and for-each? Why the map function is fast than for-each? <== is it for all case?
From: Ben Bullock on 18 Apr 2008 23:42 On Sat, 19 Apr 2008 11:30:05 +0800, mynews wrote: > Whis is difference between map and for-each? perldoc -f map > Why the map function is > fast than for-each? <== is it for all case? Could you post an example piece of code where the map function is faster than foreach?
From: J�rgen Exner on 18 Apr 2008 23:49 "mynews" <sonet.all(a)gmail.com> wrote: >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. jue
From: Gerry Ford on 19 Apr 2008 17:40 "J�rgen Exner" <jurgenex(a)hotmail.com> wrote in message news:b7qi04hcsmlovev452suvispr820t1u9n0(a)4ax.com... > "mynews" <sonet.all(a)gmail.com> wrote: >>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) They don't seem like comparables to me. Of course, there could be a lot more happening in the above line than I realize. I haven't encountered foreach as a statement modifier yet. -- "A belief in a supernatural source of evil is not necessary; men alone are quite capable of every wickedness." ~~ Joseph Conrad (1857-1924), novelist
From: Abigail on 19 Apr 2008 18:10
_ mynews (sonet.all(a)gmail.com) wrote on VCCCXLV September MCMXCIII in <URL:news:fubp0g$aa0$1(a)netnews.hinet.net>: @@ Whis is difference between map and for-each? map is a function, with a return value. foreach is a statement. @@ Why the map function is fast than for-each? <== is it for all case? Huh? Abigail -- perl -wle 'eval {die ["Just another Perl Hacker"]}; print ${$@}[$#{@${@}}]' |