in reply to Join multiple lines in a string based on regex.
I like the following way:
use strict; use warnings; use Data::Dumper; my @data; while( <DATA> ) { chop; if ( /^(array.*|unassigned)$/ ) { push @data, $1; } else { $data[-1] .= " $_"; } } print Dumper \@data; __DATA__ array A physicaldrive 2C:1:1 port 2C:box 1:bay 1 SAS 1 TB OK physicaldrive 2C:1:2 port 2C:box 1:bay 2 SAS 1 TB OK array B physicaldrive 2C:1:3 port 2C:box 1:bay 3 SAS 1 TB OK physicaldrive 2C:1:4 port 2C:box 1:bay 4 SAS 1 TB OK unassigned physicaldrive 7C:3:7 port 7C:box 3:bay 7 SAS 1 TB OK physicaldrive 7C:3:8 port 7C:box 3:bay 8 SAS 1 TB OK
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Join multiple lines in a string based on regex.
by GotToBTru (Prior) on Dec 16, 2014 at 15:25 UTC | |
|
Re^2: Join multiple lines in a string based on regex.
by pr33 (Scribe) on Dec 16, 2014 at 20:38 UTC | |
by hdb (Monsignor) on Dec 17, 2014 at 10:23 UTC | |
|
Re^2: Join multiple lines in a string based on regex.
by pr33 (Scribe) on Dec 16, 2014 at 19:46 UTC |