in reply to Manually incrementing @ array during for

Although I'm not 100% clear on what you want maybe the answer for you is to use the array index:

#!/usr/bin/env perl use strict; use warnings; my @array = (<DATA>); my $i = 0; while ($i <= $#array) { print "Line $i is $array[$i++]"; while ($i <= $#array && $array[$i] =~ /^ /) { print "Line $i is a continuation: $array[$i++]"; } } __DATA__ keyword1 data1 data2 data3 keyword2 data1 data2 data3 data4 data5 data6 keyword1 data1 data2 data3 data4 keyword3 data1

Replies are listed 'Best First'.
Re^2: Manually incrementing @ array during for
by cniggeler (Sexton) on Mar 16, 2020 at 16:24 UTC
    Thank you for the prompt reply. I was kinda hoping perl had a way to increment in the array without a shift. But changing to a while (or maybe a classic C for loop) using an index, and your reply contains a nice template!

      Generally, languages that provide implicit iteration over aggregates do not provide a way to control the implicit iterator. You will need to either preprocess the @array or use an explicit index variable.