in reply to I need to regex multiple lines
If you want lines into strings:
my ( $key, %data ); while ( <FH> ) { if ( /^([^:]+):/ ) { $data{ $key = $1 } = $_; } else { $data{ $key } .= $_; } }
If you want lines into arrays:
my ( $key, %data ); while ( <FH> ) { if ( /^([^:]+):/ ) { $data{ $key = $1 } = [ $_ ]; } else { push @{ $data{ $key } }, $_; } }
If you just want an array instead of a hash:
my ( $key, @data ); while ( <FH> ) { if ( /^([^:]+):/ ) { push @data, [ $_ ]; } else { push @{ $data[ -1 ] }, $_; } }
Update: and with a single array:
my ( $key, @data ); while ( <FH> ) { if ( /^([^:]+):/ ) { push @data, $_; } else { $data[ -1 ] .= $_; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: I need to regex multiple lines
by svenXY (Deacon) on Feb 26, 2008 at 07:54 UTC |