Here's a simple solution that doesn't worry how many lines
a record spans.
#!/usr/bin/perl -w
use strict;
sub do_stuff_with {
# Silly proof of results
my ($first, $text, $name, $last) = @_;
print "First Bit: [$first]\n";
print "Some Text: [$text]\n";
print "The Name: [$name]\n";
print "Last Bit: [$last]\n\n";
}
# Here's the meat...
my $chunk;
while ( chomp($chunk .= <DATA>) ) {
if ( $chunk =~ /([^|]+)\|([^|]+)\|([^|]+)\|([^|]+)/ ) {
my ($first, $text, $name, $last) = ($1,$2,$3,$4);
$text =~ s/\s+/ /g;
do_stuff_with($first, $text, $name, $last);
$chunk = '';
}
}
__DATA__
friend|some text here|john|any
friend|some other text here|mary|void
friend|some text here but broken up
into a few
lines|simon|any
friend|some more text|john|any
This is fairly robust (depending on how much variation there is in your data). You might want to check for an incomplete record after dropping out of the 'while' loop.
------------------------------------------------------------
"Perl is a mess
and that's good because the
problem space is also a mess." - Larry Wall