perl "alternating foreach" feature idea

E

Erik 2.0

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
 
G

Gunnar Hjalmarsson

Erik said:
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 )
 
M

Mirco Wahab

Gunnar said:
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.
 
P

Paul Lalli

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
 
J

Jürgen Exner

Erik said:
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
 
G

gf

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top