map, for, and while

Discussion in 'Perl Misc' started by J Krugman, Feb 19, 2004.

  1. J Krugman

    J Krugman Guest

    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
     
    J Krugman, Feb 19, 2004
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Feb 19, 2004
    #2
    1. Advertising

  3. J Krugman <> 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\\
     
    Brian McCauley, Feb 19, 2004
    #3
  4. J Krugman

    Anno Siegel Guest

    Brian McCauley <> wrote in comp.lang.perl.misc:
    > J Krugman <> 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
     
    Anno Siegel, Feb 19, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kl
    Replies:
    7
    Views:
    1,295
    James Kanze
    Jan 1, 2008
  2. Replies:
    1
    Views:
    429
    red floyd
    Dec 21, 2008
  3. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,027
    James Kanze
    Dec 22, 2008
  4. James Kanze
    Replies:
    0
    Views:
    2,016
    James Kanze
    Dec 21, 2008
  5. Kai-Uwe Bux
    Replies:
    1
    Views:
    1,156
    Kai-Uwe Bux
    Dec 21, 2008
Loading...

Share This Page