Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: joining lines

by dvergin (Monsignor)
on Apr 24, 2002 at 06:01 UTC ( [id://161517]=note: print w/replies, xml ) Need Help??


in reply to joining lines

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://161517]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found