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

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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 16:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found