Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Manually incrementing @ array during for

by cniggeler (Sexton)
on Mar 18, 2020 at 02:20 UTC ( [id://11114410] : note . print w/replies, xml ) Need Help??


in reply to Manually incrementing @ array during for

Thanks for the many great replies! I ended up using a while loop and index to step through the @array, and coalescing multiple lines into one line, which was then parsed. The index had to be incremented for each "extra" line so the outer while loop didn't re-process the coalesced lines.
  • Comment on Re: Manually incrementing @ array during for

Replies are listed 'Best First'.
Re^2: Manually incrementing @ array during for
by jo37 (Deacon) on Mar 18, 2020 at 16:51 UTC

    Although you already have a working solution, I took this as a puzzle where the array

    • has been filled before
    • must not be shifted or modified otherwise
    • shall be processed in a for-loop without a next statement
    Here is my (not so serious) solution:

    #!/usr/bin/perl use strict; use warnings; my @data = <DATA>; for ((my $i, local $_, my $next) = (0, @data[0, 1]); $i < @data; ($_, $next) = ($next, $data[++$i + 1])) { $next and $next =~ /^\s/ and ($_, $next) = ($_ . $next, $data[++$i + + 1]) and redo; # processing goes here print "#$i: $_"; } __DATA__ keyword1 data1 data2 data3 keyword2 data1 data2 data3 data4 data5 data6 keyword1 data1 data2 data3 data4 keyword3 data1
    which gives:
    #0: keyword1 data1 data2 data3 #3: keyword2 data1 data2 data3 data4 data5 data6 #4: keyword1 data1 data2 data3 data4 #5: keyword3 data1

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$