For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > February 2007 > perl "alternating foreach" feature idea









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author perl "alternating foreach" feature idea
Erik 2.0

2007-02-25, 8:01 am

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 w.
counting this email, and assuming the feature gets implemented, i'd be
net positive within 8 months or so.

- love
erik

Sherm Pendley

2007-02-25, 8:01 am

"Erik 2.0" <erik@q32.com> writes:

> 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


Great idea! When you've implemented it, you should send your patch to p5p
to get it integrated with mainline Perl.

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
Gunnar Hjalmarsson

2007-02-25, 8:01 am

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
Mirco Wahab

2007-02-25, 8:01 am

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.
Paul Lalli

2007-02-25, 7:03 pm

On Feb 25, 5:06 am, "Erik 2.0" <e...@q32.com> 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 w.
> 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

Brian McCauley

2007-02-25, 7:03 pm

On Feb 25, 10:25 am, Sherm Pendley <spamt...@dot-app.org> wrote:
> "Erik 2.0" <e...@q32.com> writes:
>
>
>
> Great idea! When you've implemented it, you should send your patch to p5p
> to get it integrated with mainline Perl.


I think this sort of thing in the core language is bloat. IIRC this
particular bloat is in Perl6. IMHO this is a case where we should have
KISS.

Michele Dondi

2007-02-25, 7:03 pm

On 25 Feb 2007 02:06:27 -0800, "Erik 2.0" <erik@q32.com> wrote:

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


Perl 6 supports this semantic in a syntactically very similar way:

pugs> for qw/foo 1 bar 2/ -> $a, $b { say $a ~ $b }
foo1
bar2
undef

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


I wouldn't call it "alternating foreach", but yes: it is. However you
know that ($key, $val) loudly screams for HASH, don't you?

>it would reduce the number of keystrokes i type by dozens per w.


That's strange because I do see the usefulness of the construct but
fail to see it coming out just SO useful all the time. Actually I've
never felt a compelling need for it.

>counting this email, and assuming the feature gets implemented, i'd be
>net positive within 8 months or so.


I have difficulties parsing the last phrase, care to explain?

However, as you may have discovered by now, clpmisc is hardly a place
for discussing development *of* Perl, i.e. features it *could* have
-although we all are tempted to do so from time to time-, but rather
one about development *in* Perl, i.e. features it *has*.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Jürgen Exner

2007-02-25, 7:03 pm

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


Michele Dondi

2007-02-25, 7:03 pm

On Sun, 25 Feb 2007 05:25:08 -0500, Sherm Pendley
<spamtrap@dot-app.org> wrote:

>Great idea! When you've implemented it, you should send your patch to p5p
>to get it integrated with mainline Perl.


I doubt that that idea, with that syntax could ever make its way into
Perl 5, and I cannot think of an easy modification that would make it
fit into current Perl syntax for C<for>.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Sherm Pendley

2007-02-25, 7:03 pm

Michele Dondi <bik.mido@tiscalinet.it> writes:

> On Sun, 25 Feb 2007 05:25:08 -0500, Sherm Pendley
> <spamtrap@dot-app.org> wrote:
>
>
> I doubt that that idea, with that syntax could ever make its way into
> Perl 5, and I cannot think of an easy modification that would make it
> fit into current Perl syntax for C<for>.


What I was really getting at was that first, p5p is a better place to talk
about additions to Perl than clpm, and second that code speaks far louder
there than ideas.

Apparently dry sarcasm doesn't translate very well to ASCII... :-\

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
Michele Dondi

2007-02-25, 7:03 pm

On Sun, 25 Feb 2007 13:16:53 -0500, Sherm Pendley
<spamtrap@dot-app.org> wrote:

>
>What I was really getting at was that first, p5p is a better place to talk
>about additions to Perl than clpm, and second that code speaks far louder
>there than ideas.


No, no, I understood perfectly well your intent. Only, the OP may have
been tempted to really do so, or whatever. So for sake of completeness
I "pretended" you were being serious.

>Apparently dry sarcasm doesn't translate very well to ASCII... :-\


Naaah, it *does* translate well enough. Most of times, that is.
Actually I wanted to include in my reply something along the lines of
"I know this is just a reminder that this is not the best place to..."
but I didn't out of the desire to stay concise, for one - generally I
plainly can't!


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
gf

2007-02-26, 7:05 pm

On Feb 25, 3:06 am, "Erik 2.0" <e...@q32.com> 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 w.
> 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.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com