I have a foreach loop something like
foreach my $ref_array (@$array1)
where $array is the result of reading an entire Excel sheet.
Inside my loop $ref_array gets the value of each row in the sheet. Now I want to advance $ref_array such that it gets the value of next row of the spreadsheet. How shall I do it in the middle of the loop?
Looping from 0 to the last array index $#$array1 would allow you to access the next row/element easily:
for my $index ( 0 .. $#$array1 ) {
my ( $current, $next ) = @$array1[ $index, $index + 1 ];
# Process the rows
}
A Perl 5.12.0+ alternative:
for ( my ( $idx, $row ) = each @$array1 ) {
last if $idx == $#array1; # Skip last iteration
my $next_row = $array1->[$idx+1];
# ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With