Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

foreach and map..how are they different?
Aside from the syntax, is there a difference in the way 'map' and
'foreach' process?


Report this thread to moderator Post Follow-up to this message
Old Post
Dan Sopher
08-25-07 12:01 AM


Re: foreach and map..how are they different?
>>>>> ""Dan" == "Dan Sopher" <dsopher@twistbox.com> writes:

"Dan> Aside from the syntax, is there a difference in the way 'map' and
"Dan> 'foreach' process?

Yes.  map is an expression.  foreach is a statement.  foreach can't
be nested inside a larger expression.  map is *meant* to do that,
and using it in a void context is generally frowned upon.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training
!

Report this thread to moderator Post Follow-up to this message
Old Post
Randal L. Schwartz
08-25-07 12:01 AM


Re: foreach and map..how are they different?
On Aug 24, 2:21 pm, dsop...@twistbox.com (Dan Sopher) wrote:
> Aside from the syntax, is there a difference in the way 'map' and
> 'foreach' process?

foreach is just a loop.  map returns values.  Specifically, it returns
the value of the BLOCK/EXPR for each iteration of the loop.

my @doubles = map { $_ * 2 } @nums;

is exactly equivalent to:

my @doubles;
foreach (@nums) {
push @doubles, $_ * 2;
}

Paul Lalli


Report this thread to moderator Post Follow-up to this message
Old Post
Paul Lalli
08-25-07 12:01 AM


Re: foreach and map..how are they different?
On 8/24/07, Dan Sopher <dsopher@twistbox.com> wrote:
> Aside from the syntax, is there a difference in the way 'map' and
> 'foreach' process?
snip

There are many differences, in addition to what has already been said
map can be more or less cpu efficient than depending on the task*.  If
you can use map to avoid  a temporary array, then it can save you some
time, but if you need the intermediate result, then for is faster.
Personally, I prefer map (and its cousin grep) for things that can be
done in one line:

my @files = map { $_->{filename} } $sftp->ls();

rather than

my @files;
push @files, $_->{filename} for $sftp->ls();

But if the code being passed to map becomes to complex, I prefer a for loop:

my @headers;
for my $filename (@filenames) {
open my $file, "<", $filename
or die "could not open $filename:$!";
push @headers, scalar <$file>;
}

rather than

my @headers = map { open my $f, "<", $_ or die "could not open $_:$!";
scalar <$_>; } @filenames;


* Here are the benchmarks for a two different scenarios

Map vs for vs map with temp variable

Rate map_with_var          for          map
map_with_var 3.51/s           --         -11%         -57%
for          3.92/s          12%           --         -52%
map          8.11/s         131%         107%           --

#!/usr/bin/perl

use strict;
use warnings;
use Benchmark;

my @a = 1 .. 1_000_000;
my %subs = (
for => sub {
my @b;
push @b, ($_ + 4) for @a;
return @b;
},
map => sub {
return map { $_ + 4 } @a;
},
map_with_var => sub {
my @b = map { $_ + 4 } @a;
return @b;
}
);

Benchmark::cmpthese(-1, \%subs);

Map of Map vs Two for loops vs Map of Map with temp variable vs map of
map with two temp variables:

Rate map_with_2var           for  map_with_var           map
map_with_2var 1793/s            --           -8%          -16%          -45%
for           1950/s            9%            --           -8%          -40%
map_with_var  2124/s           18%            9%            --          -35%
map           3258/s           82%           67%           53%            --

#!/usr/bin/perl

use strict;
use warnings;
use Benchmark;

my @a = 1 .. 1_000;
my %subs = (
for => sub {
my @b;
push @b, ($_ + 4) for @a;
my @c;
push @c, ($_ + 4) for @b;
return @b;
},
map => sub {
return map { $_ + 4 } map { $_ + 4 } @a;
},
map_with_var => sub {
my @b = map { $_ + 4 } map { $_ + 4 } @a;
return @b;
},
map_with_2var => sub {
my @b = map { $_ + 4 } @a;
my @c = map { $_ + 4 } @b;
return @c;
}
);

Benchmark::cmpthese(-1, \%subs);

Report this thread to moderator Post Follow-up to this message
Old Post
Chas Owens
08-25-07 12:01 AM


Re: foreach and map..how are they different?
Dan Sopher wrote:
>
> Aside from the syntax, is there a difference in the way 'map' and
> 'foreach' process?

Hi Dan

Internally they're very similar, but you shouldn't be thinking like that.

As Randal said, foreach is a statement - a language construct like 'if',
'while', 'else' and so on - while map is an expression - more specifically
a function.

Use foreach if you want to execute a block of Perl code for every element
in a list.

Use map to implement a /mapping/ between two lists. It takes an input list
and a statement or a block specifying a transformation, and (in list
context) returns the list with that transformation applied to each element.
Conceptually there is no loop - the entire list is transformed at once, and
a scalar expression like

$a = 2 * $b

corresponds exactly to the list expression

@a = map 2 * $_, @b

HTH,

Rob


Report this thread to moderator Post Follow-up to this message
Old Post
Rob Dixon
08-25-07 12:01 AM


Re: foreach and map..how are they different?
Both map and foreach can do the same thing,though they're used in
different syntax environment.
I follow a rule,if you need to return a result list,use map.Otherwise
use for/foreach.
Also sometime we can use map to do some flexible translation,like
Schwartz Translation.


2007/8/25, Dan Sopher <dsopher@twistbox.com>:
> Aside from the syntax, is there a difference in the way 'map' and
> 'foreach' process?
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Pang
08-25-07 02:59 AM


Re: foreach and map..how are they different?
>>>>> ""Jeff" == "Jeff Pang" <rwwebs@gmail.com> writes:

"Jeff> Also sometime we can use map to do some flexible translation,like
"Jeff> Schwartz Translation.

That's a new one for me.  Are you trying to say "Schwartzian Transform"?

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training
!

Report this thread to moderator Post Follow-up to this message
Old Post
Randal L. Schwartz
08-25-07 09:00 AM


Re: foreach and map..how are they different?
2007/8/25, Randal L. Schwartz <merlyn@stonehenge.com>: 
>
> "Jeff> Also sometime we can use map to do some flexible translation,like
> "Jeff> Schwartz Translation.
>
> That's a new one for me.  Are you trying to say "Schwartzian Transform"?
>

Yes,sorry for spelling wrong.You're right.:)

Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Pang
08-25-07 09:00 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:06 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.