Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: Manually incrementing @ array during for

by Fletch (Bishop)
on Mar 16, 2020 at 18:33 UTC ( [id://11114360]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Manually incrementing @ array during for
in thread Manually incrementing @ array during for

Very similar approach in that case, just instead of reading lines from the file you walk the indexen instead. The concatenation and processing of entries is similar otherwise (my sample changes only 4 lines).

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^4: Manually incrementing @ array during for
by AnomalousMonk (Archbishop) on Mar 16, 2020 at 20:20 UTC

    Note that the body of this while-loop is equally well suited to the original for-loop implementation. The array is not changed.

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @ra = ( 'keyword1 data1 data2 data3', 'keyword2 data1 data2 data3', ' data4 data5', ' data6', 'keyword1 data1 data2 data3 data4', 'keyword3 data1', ); ;; my $rx_continuation = qr{ \A \s+ \w }xms; ;; my $current_line; LINE: for my $line (@ra) { if ($line =~ $rx_continuation) { $current_line .= $line; next LINE; } ;; _process($current_line) if $current_line; $current_line = $line; } ;; _process($current_line) if $current_line; ;; dd \@ra ;; ;; sub _process { print qq{'$_[0]' ->}, map qq{ '$_'}, split ' ', $_[0]; } " 'keyword1 data1 data2 data3' -> 'keyword1' 'data1' 'data2' 'data3' 'keyword2 data1 data2 data3 data4 data5 data6' -> 'keyword2' 'da +ta1' 'data2' 'data3' 'data4' 'data5' 'data6' 'keyword1 data1 data2 data3 data4' -> 'keyword1' 'data1' 'data2' 'data +3' 'data4' 'keyword3 data1' -> 'keyword3' 'data1' [ "keyword1 data1 data2 data3", "keyword2 data1 data2 data3", " data4 data5", " data6", "keyword1 data1 data2 data3 data4", "keyword3 data1", ]


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-26 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found