use strict;
use warnings;
my @SampleArray = ("one", "two", "three");
for my $ArrayIndex ( 0 .. $#SampleArray ) {
printf(
"%s is at location %d in the array\n",
$SampleArray[$ArrayIndex],
$ArrayIndex,
);
}
Or just use a sufficiently modern version of Perl...
use v5.12;
use strict;
use warnings;
my @SampleArray = ("one", "two", "three");
while (my ($ArrayIndex, $ArrayItem) = each @SampleArray) {
printf(
"%s is at location %d in the array\n",
$ArrayItem,
$ArrayIndex,
);
}
Update: Although each @array was only added to Perl as of 5.12, Array::Each::Override provides a working implementation of each @array for Perl going back to 5.8.
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
|