From: Tad J McClellan on
Gerry Ford <gerry(a)nowhere.ford> wrote:
>
> "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)


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.noitatibaher\100cmdat/"
From: nolo contendere on
On Apr 18, 11:49 pm, Jürgen Exner <jurge...(a)hotmail.com> wrote:
> "mynews" <sonet....(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.
Is this still true? I thought map in void context was optimized.
From: Gerry Ford on

"Tad J McClellan" <tadmc(a)seesig.invalid> wrote in message
news:slrng0mhgh.ujm.tadmc(a)tadmc30.sbcglobal.net...
> Gerry Ford <gerry(a)nowhere.ford> wrote:
>>

>> 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.

Maybe we can use this script to compare and contrast:
sub read_killrc {
open(FILE, "<$killrc") and do {
@filter = ();
foreach (<FILE>) {
chomp; length or next; /^#/o and next;
my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
push @filter, $pat;
}
close(FILE);
};
}

You usually tell me to read when I ask a question. I rebound the camel book
today with plywood strips and tie wire: as good as it was before I tore it
apart and flung it at the wall. I simply *didn't get* some of the examples.
Would you expect the @filter to be a list of regex's here?

What would this syntax look like with a map function?

--
"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: Uri Guttman on
>>>>> "GF" == Gerry Ford <gerry(a)nowhere.ford> writes:

GF> Maybe we can use this script to compare and contrast:
GF> sub read_killrc {
GF> open(FILE, "<$killrc") and do {
GF> @filter = ();

not needed in the map version

GF> foreach (<FILE>) {
GF> chomp; length or next; /^#/o and next;
GF> my $pat; eval '$pat = qr/$_/' or do {prompt $@; next};
GF> push @filter, $pat;
GF> }
GF> close(FILE);

GF> What would this syntax look like with a map function?

untested:

return unless open(my $file, "<$killrc") ;
return map {
/^([^#]+)#?$/ &&
eval{ qr/$1/} || prompt $@, ()
} <$file> ;

maybe the return failure value should be grepped out but that is a minor
style change.

map makes tight loops like that much cleaner. they remove the collecting
array and its push and uses less perl which generally makes it faster
(in the right conditions, especially with shorter arrays)

map is such a basic concept i wonder why it seems to be a stumbling
block for so many newbies. year after year we get the same map
queries. what use is it? how do i convert loops to maps? is it faster or
slower? void map is ok? some are in the faq but there must be something
missing if this perl learning speed bump always seems to hit so many.

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Ben Bullock on
On Sun, 20 Apr 2008 15:30:06 -0700, nolo contendere wrote:

> On Apr 18, 11:49 pm, Jürgen Exner <jurge...(a)hotmail.com> wrote:

>> 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.

> Is this still true? I thought map in void context was optimized.

There was a discussion about this topic a month ago (started March 22).
Perhaps reading this might help:

http://groups.google.co.jp/group/comp.lang.perl.misc/search?hl=en&q=map
+void+context&start=0&scoring=d&hl=en&