Perl equivalent to Php foreach loop
foreach, perl, php
Solution
If you're using Perl 5.12.0 or above you can use `each` on arrays:
my @array = 100 .. 103;
while (my ($key, $value) = each @array) {
print "$key\t$value\n";
}
Output:
0 100
1 101
2 102
3 103
perldoc each
Problem
I am looking for a Perl equivalent to the following php code:- ``` foreach($array as $key => $value){ ... } ``` I know I can do a foreach loop like so:- ``` foreach my $array_value (@array){ .. } ``` Which will enable me to do things with the array values - but I would like to use the keys as well. I know there is a Perl hash which allows you to set up key-value pairs, but I just want the index number that the array automatically gives you.