Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > map, for, and while

Reply
Thread Tools

map, for, and while

 
 
J Krugman
Guest
Posts: n/a
 
      02-19-2004




With map, one can streamline for-loops like this one:

my @array_2;
push @array_2, some_sub($_) for @array_1;

into a single assignment:

my @array_2 = map some_sub($_), @array_1;

Is there a way (not necessarily using map) to streamline while-loops
like this one:

my @array;
push @array, some_other_sub() while some_condition();

into a single assignment to @array?

TIA!

jill


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-19-2004
J Krugman wrote:
> With map, one can streamline for-loops like this one:
>
> my @array_2;
> push @array_2, some_sub($_) for @array_1;
>
> into a single assignment:
>
> my @array_2 = map some_sub($_), @array_1;
>
> Is there a way (not necessarily using map) to streamline
> while-loops like this one:
>
> my @array;
> push @array, some_other_sub() while some_condition();
>
> into a single assignment to @array?


How about using map()?

my @array = map some_sub($_), <FH>;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      02-19-2004
J Krugman <(E-Mail Removed)> writes:

> With map, one can streamline for-loops like this one:
>
> my @array_2;
> push @array_2, some_sub($_) for @array_1;
>
> into a single assignment:
>
> my @array_2 = map some_sub($_), @array_1;
>
> Is there a way (not necessarily using map) to streamline while-loops
> like this one:
>
> my @array;
> push @array, some_other_sub() while some_condition();
>
> into a single assignment to @array?


I don't think you'll find a way that's more efficient.

Anyhow, the subroutine-call overhead (let alone whatever's in the
subroutines) is likely to be the largest part of the time so there's
no point worrying about finding a better way to concatenate the return
values from some_other_sub().

#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;

my $i;
sub some_other_sub { 1 }
sub some_condition { $i-- }

timethese(1000,{
subs => sub {
my @array;
$i = 1000;
push @array, some_other_sub() while some_condition();
},
nosubs => sub {
my @array;
$i = 1000;
push @array, 1 while $i--;
},
});
__END__
Benchmark: timing 1000 iterations of nosubs, subs...
nosubs: 1 wallclock secs ( 0.76 usr + 0.00 sys = 0.76 CPU) @ 1315.79/s (n=1000)
subs: 3 wallclock secs ( 2.88 usr + 0.00 sys = 2.88 CPU) @ 347.22/s (n=1000)

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-19-2004
Brian McCauley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> J Krugman <(E-Mail Removed)> writes:
>
> > With map, one can streamline for-loops like this one:
> >
> > my @array_2;
> > push @array_2, some_sub($_) for @array_1;
> >
> > into a single assignment:
> >
> > my @array_2 = map some_sub($_), @array_1;
> >
> > Is there a way (not necessarily using map) to streamline while-loops
> > like this one:
> >
> > my @array;
> > push @array, some_other_sub() while some_condition();
> >
> > into a single assignment to @array?

>
> I don't think you'll find a way that's more efficient.


I think it's more convenience than efficiency what Jill(?) is after.
Also, some_other_sub() and some_condition() are probably just examples
for arbitrary expressions.

I guess the idea is the mythical "map ... while", which would allow the
above to be written

map some_other_sub() while some_condition();

(Obviously, "map ... while" is a syntactical construct whose behavior
can't be derived from the normal behavior of "map" and "while".)

> Anyhow, the subroutine-call overhead (let alone whatever's in the


[snip benchmark which compared these:]

> push @array, some_other_sub() while some_condition();
> push @array, 1 while $i--;


my @array = map 1 while $i--;

That would beat them all

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between while and do while Logan Lee C Programming 9 12-25-2007 12:26 PM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Help while error "Error while trying to run project:" David ASP .Net 1 07-19-2004 08:41 PM
Got error msg while Debugging : Error while trying to run project: ... ^CrazyCoder^ ASP .Net 3 09-15-2003 09:40 AM



Advertisments