in reply to Perl Problems with Matching Expression Patterns
You have records with two fields seperated by colon. You can just split on that to get the arrangement you want. Putting that in a hash is attractive, but doesn't preserve order, so lets make it an array of arrays instead. You probably want to trim leading and trailing whitespace, too.
The split on colon does the field seperation, then the mapped match trims leading and trailing space. Square brackets make the result an array reference.for (@DATA) { # this transforms @DATA, destructive $_ = [map {/^\s*(.*?)\s*$/} split ':', $_, 2]; }
To print the new @DATA in the form you want,
for (@DATA) { printf "FIRST TERM - %s\nSECOND TERM - %s\n", @$_; }
Update: holli++, I did miss that. We do see what we expect, don't we? Second try,
That's simpler to read, too.for (@DATA) { $_ = join ' ', split; # normalize whitespace $_ = [split /[:\s]/, $_, 2]; }
After Compline,
Zaxo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl Problems with Matching Expression Patterns
by holli (Abbot) on Jan 12, 2005 at 21:42 UTC |