February 2010 (1)
September 2009 (1)
May 2009 (1)
April 2009 (1)
March 2009 (4)
January 2009 (3)
November 2008 (2)
October 2008 (2)
September 2008 (1)
August 2008 (5)
July 2008 (3)
June 2008 (1)
May 2008 (5)
April 2008 (8)
March 2008 (3)
February 2008 (1)
January 2008 (2)
December 2007 (2)
November 2007 (4)
October 2007 (17)
September 2007 (9)
2008-04-10 07:23:16
Python has the rather nifty enumerate for those times when you want to iterate over a sequence with an index:
>>> l = ['spam', 'eggs', 'spam']
>>> for (i, j) in enumerate(l):
... print i, j
...
0 spam
1 eggs
2 spam
I’m enjoying reawakening those dormant brain cells where the Perl lives, but I miss these little niceties.
From Sam Vilain on 2008-04-21 10:50:47
CPAN to the rescue. Using Array::Each::Override
use Array::Each::Override;
my @array = qw(a b c);
while (my ($i, $val) = each @array) {
print “Position $i contains: $val\n”;
}
Rendered at 2010-08-01 22:30:17
From Michael on 2008-04-10 09:24:31
use List::MoreUtils qw!pairwise!;
@l = qw!spam eggs spam!;
pairwise { print “$a $b\n” } (0..scalar @l), @l;
OK, not as obvious, and not as builtin, but it’s there!