Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Manually incrementing @ array during for

by hippo (Bishop)
on Mar 16, 2020 at 16:05 UTC ( [id://11114349]=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114349]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-14 17:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found