perl "alternating foreach" feature idea

Discussion in 'Perl Misc' started by Erik 2.0, Feb 25, 2007.

  1. Erik 2.0

    Erik 2.0 Guest

    dear perl people,

    in the spirit of both perl's brevity and it's "do what i mean" array
    contexts, it would be an appropriate feature for foreach loops to
    support an array of variables as the iterator as well as just a
    scalar. this would cause perl to alternate across the array:

    for example:

    foreach ($key, $val) in (@pairs) {
    print "$key=$val\n";
    }

    an odd number of values in @pairs would cause an extra iteration with
    $val set to undef

    this syntax for an "alternating foreach" seems quite elegant to me.
    it would reduce the number of keystrokes i type by dozens per week.
    counting this email, and assuming the feature gets implemented, i'd be
    net positive within 8 months or so.

    - love
    erik
    Erik 2.0, Feb 25, 2007
    #1
    1. Advertising

  2. Erik 2.0 wrote:
    > it would be an appropriate feature for foreach loops to
    > support an array of variables as the iterator as well as just a
    > scalar. this would cause perl to alternate across the array:
    >
    > for example:
    >
    > foreach ($key, $val) in (@pairs) {
    > print "$key=$val\n";
    > }
    >
    > an odd number of values in @pairs would cause an extra iteration with
    > $val set to undef


    What's wrong with:

    while ( ($key, $val) = splice @pairs, 0, 2 )

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Feb 25, 2007
    #2
    1. Advertising

  3. Erik 2.0

    Mirco Wahab Guest

    Gunnar Hjalmarsson wrote:
    > What's wrong with:
    >
    > while ( ($key, $val) = splice @pairs, 0, 2 )


    1) its not 'for'
    2) it kills @pairs

    consider a somehow 'generalized' form:

    use strict;
    use warnings;

    sub shift_1{my @r; push@r,[shift@_]while@_;@r}
    sub shift_2{my @r; push@r,[shift@_,shift@_]while@_;@r}
    sub shift_3{my @r; push@r,[shift@_,shift@_,shift@_]while@_;@r}


    my @array =qw' 1 2 3 4 5 6 7 8 9 ';

    for my $ar ( shift_3 @array ) {
    print $_ for @$ar, "\n";
    }



    Regards

    M.
    Mirco Wahab, Feb 25, 2007
    #3
  4. Erik 2.0

    Paul Lalli Guest

    On Feb 25, 5:06 am, "Erik 2.0" <> wrote:
    > dear perl people,
    >
    > in the spirit of both perl's brevity and it's "do what i mean" array
    > contexts, it would be an appropriate feature for foreach loops to
    > support an array of variables as the iterator as well as just a
    > scalar. this would cause perl to alternate across the array:
    >
    > for example:
    >
    > foreach ($key, $val) in (@pairs) {
    > print "$key=$val\n";
    >
    > }
    >
    > an odd number of values in @pairs would cause an extra iteration with
    > $val set to undef
    >
    > this syntax for an "alternating foreach" seems quite elegant to me.
    > it would reduce the number of keystrokes i type by dozens per week.
    > counting this email, and assuming the feature gets implemented, i'd > be net positive within 8 months or so.


    perl -MList::MoreUtils=natatime -le'
    my @pairs = qw/1 a 2 b 3 c 4 d/;
    my $it = natatime 2, @pairs;
    while (my ($key, $val) = $it->()){
    print "$key = $val";
    }
    '
    1 = a
    2 = b
    3 = c
    4 = d


    Paul Lalli
    Paul Lalli, Feb 25, 2007
    #4
  5. Erik 2.0 wrote:
    > dear perl people,
    >
    > in the spirit of both perl's brevity and it's "do what i mean" array
    > contexts, it would be an appropriate feature for foreach loops to
    > support an array of variables as the iterator as well as just a
    > scalar. this would cause perl to alternate across the array:
    >
    > for example:
    >
    > foreach ($key, $val) in (@pairs) {
    > print "$key=$val\n";
    > }



    This looks like a poor design of your data structure. If you sequence has
    alternating elements of identifiers (aka keys) and values, then an AoA seems
    to be a much more natural way to represent this data.
    And iterating over an array that contains just the references to the data
    pairs works beautifully with the existing Perl features as of today.

    > this syntax for an "alternating foreach" seems quite elegant to me.


    Well, but your data structure is anything but.

    jue
    Jürgen Exner, Feb 25, 2007
    #5
  6. Erik 2.0

    gf Guest

    On Feb 25, 3:06 am, "Erik 2.0" <> wrote:

    > foreach ($key, $val) in (@pairs) {
    > print "$key=$val\n";
    >
    > }


    [...]

    > this syntax for an "alternating foreach" seems quite elegant to me.
    > it would reduce the number of keystrokes i type by dozens per week.
    > counting this email, and assuming the feature gets implemented, i'd be
    > net positive within 8 months or so.


    To me, "elegance" in programming means doing something powerful within
    the constraints of whatever language I'm working in. Not in changing
    the language.

    Try thinking in Perl terms...

    use strict;
    use warnings;

    my @a = qw( one 1 two 2 three 3 four 4 five 5 six 6 seven 7 eight 8
    nine 9 ten 10 );
    my ( $b, $c );
    for ( my $i = 0 ; ( $b, $c ) = @a[ $i, $i + 1 ], $i < scalar(@a) ; $i
    += 2 )
    {
    print $b, ' => ', $c, "\n";
    }

    one => 1
    two => 2
    three => 3
    four => 4
    five => 5
    six => 6
    seven => 7
    eight => 8
    nine => 9
    ten => 10

    If keystrokes equate to programming efficiency then you might want to
    look into an editor that understands abbreviations or templates or
    macros. I'll suggest vim, but that's because it's my particular
    poison.
    gf, Feb 26, 2007
    #6
    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. Replies:
    10
    Views:
    1,222
    Big K
    Feb 2, 2005
  2. Christopher M.

    Idea for a browser feature

    Christopher M., Mar 26, 2007, in forum: HTML
    Replies:
    10
    Views:
    594
    Adrienne Boswell
    Mar 30, 2007
  3. Dr Mephesto

    App idea, Any idea on implementation?

    Dr Mephesto, Feb 4, 2008, in forum: Python
    Replies:
    3
    Views:
    702
    Dennis Lee Bieber
    Feb 5, 2008
  4. Replies:
    0
    Views:
    626
  5. Yash

    #define-like feature in Perl

    Yash, Sep 29, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    104
    Joe Smith
    Sep 30, 2004
Loading...

Share This Page