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).
#!/usr/bin/env perl use 5.018; use JSON::XS qw( encode_json ); my @lines = <DATA>; my $current_line = q{}; my $idx = 0; while ( $idx <= $#lines ) { my $line = $lines[$idx]; chomp($line); if ( $line =~ m{^ \s+ \w+ }x ) { $current_line .= $line; } else { if ($current_line) { _process_line($current_line); } $current_line = $line; } $idx++; } if ($current_line) { _process_line($current_line); } sub _process_line { my ($line) = shift; my ( $kwd, @vals ) = split( /\s+/, $line ); say qq{keyword '$kwd' has }, scalar @vals, qq{ values: }, encode_json( \@vals ); } exit 0; __END__ keyword1 data1 data2 data3 keyword2 data1 data2 data3 data4 data5 data6 keyword1 data1 data2 data3 data4 keyword3 data1
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 |