Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

append line to previous line in txt file

by mmittiga17 (Scribe)
on Jul 09, 2008 at 05:07 UTC ( [id://696381]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have a file that I need to read in and if a line starts with ; append it to the previous line and print out. The code I have so far is failing miserably
while (<>) { $line = $_; chomp $line; print $line ; next; if ($line =~/^;/){ print "$line\n"; } else { print "$line\n"; } }
Any help will be appreciated.

Replies are listed 'Best First'.
Re: append line to previous line in txt file
by ysth (Canon) on Jul 09, 2008 at 05:41 UTC
    Your "next;" makes it skip the rest of the while loop body. Your if has the same thing in both branches?? How about something like this (in the cb, the requirement to remove the ; was revealed):
    my $prev; while (my $line = <>) { chomp $line; print $prev, "\n" if defined $prev && $line !~ s/^;/$prev/; $prev = $line; } print $prev, "\n" if defined $prev;
      Awesome that worked... Thanks for your help
Re: append line to previous line in txt file
by prasadbabu (Prior) on Jul 09, 2008 at 05:36 UTC

    Hi mmittiga17,

    If I understood your requirement correctly, you can do it in the below method, iff your file size is not huge.

    use strict; use warnings; my $file = do {local $/, <DATA>}; #read the file from <DATA> $file =~ s/\n\;/\;/g; #replace newline followed by semicolon with semi +colon print $file; output: -------- asdfadfasdf asfadfasdf;asfadfasdfas fasfasdfas;asdfasdfasd asdfasdfasfd __DATA__ asdfadfasdf asfadfasdf ;asfadfasdfas fasfasdfas ;asdfasdfasd asdfasdfasfd

    Prasad

Re: append line to previous line in txt file
by pc88mxer (Vicar) on Jul 09, 2008 at 06:28 UTC
    How about:
    do { $n = chomp; print; $_ = <>; print $/ x $n unless s/^;//; } while defined($_);
    Works with empty files and files that begin with a ;

    Update: This is just another way to think about the problem. You don't have to start every file reading loop with while (<>).

    Update 2: Changed unless /^;/ to unless s/^;// which addresses concerns about it not working with consecutive lines beginning with a semi-colon.

      Your code doesn't seem to cope with consecutive lines starting with a semi-colon and it gets a bit noisy running with use warnings; against an empty file. The following seems to work.

      use strict; use warnings; my $acc; while ( <> ) { chomp; if ( m{^;} ) { $acc .= substr $_, 1; } else { print $acc, qq{\n} if defined $acc; $acc = $_; } } print $acc, qq{\n} if defined $acc;

      I hope this is of interest.

      Cheers,

      JohnGG

      Update: added defined to test to cope with lines that are empty. My test file is

      ;ddadadwaew sddjdsoeugfuiegf euefuefuerfefg ;erreuruyegfegr ueyrueuefeyr7e ; ; erieihir eiereyrgueyrgh ;ieruherueu ;ierhueryugr reieruyeruy
        Your code doesn't seem to cope with consecutive lines starting with a semi-colon
        I don't understand - I think it works just fine in that case. For example, on your test file the output is:
        ;ddadadwaew sddjdsoeugfuiegf euefuefuerfefg;erreuruyegfegr ueyrueuefeyr7e ;; erieihir eiereyrgueyrgh;ieruherueu;ierhueryugr reieruyeruy
        Did you get a different result?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found