Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Perl equivalent of Python's enumerate?

Tags:

python

perl

I'm looking for a function in Perl 5 that works similarly to Python's enumerate built-in. It would return a list of references to arrays, where each array is [$index, $element]:

@a = ("a", "b", "c");
@b = enumerate @a;
# @b = ([0, "a"], [1, "b"], [2, "c"])

List::Util and List::MoreUtils don't seem to have this function. Is there another module that does?

like image 632
Flimm Avatar asked Mar 05 '15 14:03

Flimm


3 Answers

enumerate returns an iterator, not a list, so you should really be asking for an iterator.


In Perl 5.12.0 and up, you can use each to iterate over arrays:

use strict;
use warnings 'all';
use 5.012;

my @a = qw(foo bar baz);

while (my ($i, $v) = each @a) {
    say "$i -> $v";
}

__END__
0 -> foo
1 -> bar
2 -> baz

However, you should be very careful when using each; some people even discourage its use altogether.

like image 190
ThisSuitIsBlackNot Avatar answered Nov 14 '22 22:11

ThisSuitIsBlackNot


You can use map, like this

my @data = qw / a b c /;
my @enumeration = map [ $_, $data[$_] ], 0 .. $#data;
like image 45
Borodin Avatar answered Nov 14 '22 22:11

Borodin


Perl doesn't have a built-in function to do that but it's easy to roll your own.

Using map:

my @a = qw(a b c);
my $i = 0;
my @b = map [$i++, $_], @a; # ([0, 'a'], [1, 'b'], [2, 'c'])

As of v5.20, Perl's new slice syntax does something similar:

my @a = qw(a b c);
my @b = %a[0..$#a]; # (0, 'a', 1, 'b', 2, 'c')

That slice syntax returns a list of index/value pairs but it's a flat list. The pairs aren't grouped into nested arrays. If that's important to your application you can use the pairmap function from List::Util to do it:

use List::Util qw(pairmap);
my @a = qw(a b c);
my @b = pairmap {[$a, $b]} %a[0..$#a]; # ([0, 'a'], [1, 'b'], [2, 'c'])
like image 7
Michael Carman Avatar answered Nov 14 '22 20:11

Michael Carman