Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

joining lines

by Anonymous Monk
on Apr 23, 2002 at 14:41 UTC ( #161322=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have the following text file: 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 Question: how do I use perl to rejoin line 3,4,5 so that it becomes on +e line? thanking you in advance, Vincent

Replies are listed 'Best First'.
Re: joining lines
by jeffenstein (Friar) on Apr 23, 2002 at 14:56 UTC

    If it will always be a constant number of fields per line, you could do something like this:

    while(<>){ chomp; my @line = split /\|/; while( @line < 4 ){ $_ .= <>; chomp; @line = split /\|/; } if( @line > 4){ warn "Found line with too many elements"; } print join "|", @line; print "\n"; }
Re: joining lines
by darksym (Beadle) on Apr 23, 2002 at 14:56 UTC
    One strategry is to simply avoid rejoining them and parse on the |. That way newlines wouldn't matter. You can try changing the field seperator and see if it doesn't take care of this all for you, or you could use split or a regex across multiple lines not taking into account the \n character to grab your fields.

    It appears as though you are going 4 fields to a record with the field seperator of '|'.
Re: joining lines
by ehdonhon (Curate) on Apr 23, 2002 at 15:01 UTC

    Hello, Anonymous Monk

    This sounds an aweful lot like a homework problem, which monks hate to answer (especially when the poster does not fess up and admit that its homework)

    I suggest you try not posting anonymously, and also let us know what you have tried so far. You are more likely to get assistance in fixing something that is not working for you than you are to get somebody to solve the entire problem for you.

      Sorry if I offended anyone. I did have the code, but I am afraid that I was using too much code to do a simple task. I am new to perl and I was desperate to find an answer. Sorry. I will register myself.
Re: joining lines
by dvergin (Monsignor) on Apr 24, 2002 at 06:01 UTC
    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

Re: joining lines
by giulienk (Curate) on Apr 23, 2002 at 17:49 UTC
    Alternative way:
    my $line; while (defined($line=<FH>)){ chomp $line; unless (3 <= $line =~ tr,\|,\|,) { $line .= <FH>; redo unless eof(FH) } print "$line\n"; }

    $|=$_='1g2i1u1l2i4e2n0k',map{print"\7",chop;select$,,$,,$,,$_/7}m{..}g

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2021-12-06 22:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (33 votes). Check out past polls.

    Notices?