On 09/25/2006 06:47 AM, John W. Krahn wrote:
Mumia W. wrote:
On 09/24/2006 07:04 PM, David Gilden wrote:
I am having a little trouble understanding matching and getting the
sub pattern saved to a Var. so that I can do a munge. I want to take
the line returns and change them into pipe characters '|'   All data
records start with a date i.e. 01/01/2006  But there are fields in
between that are one multiple lines. Sample data is below the script.
If the file is small enough to slurp into memory, you could do something
like this:

#!/usr/bin/perl

use strict;
use warnings;

my $data = join '', <DATA>;
my @blocks = $data =~ m/^([0-9\/]+.*?(?=^[0-9\/]+))/gsm;
print "\n";

s/\n/\|/g for @blocks;
print "$_\n\n" for @blocks;

Your code puts a | at the end of the record as well as between the fields and
it doesn't remove trailing whitespace from the fields and it skips the last
record.  To fix:

my $data = do { local $/; <DATA> };
chomp( my @blocks = $data =~ m!^([\d/]+.*?(?=^[\d/]+|\z))!gsm );
print "\n";

s/\s*\n/|/g, s/\s*\z// for @blocks;
print "$_\n\n" for @blocks;




John

Uhhh. I should've looked more closely at my output. Thanks.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to