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

    Nice! Except the groups beginning with "unassigned" should not be printed.

    1 Peter 4:10
Re^2: Join multiple lines in a string based on regex.
by pr33 (Scribe) on Dec 16, 2014 at 20:38 UTC
    Thanks for the reply . Tried the same code , But it fails with the Error Below . Modification of non-creatable array value attempted, subscript -1 at ./file2.pl line 15, <$fd> line 1.

      This can happen if the data does not start with an "array" line (I am guessing).

Re^2: Join multiple lines in a string based on regex.
by pr33 (Scribe) on Dec 16, 2014 at 19:46 UTC
    Thanks for the reply . $row is the variable where my Command output file handle is for reading . How do I go about capturing the output in the $row variable as I have trimmed lot of lines in the O/P using the scalar variable .