Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: duplicating the rows and joining three lines into one using perl script

by graff (Chancellor)
on Oct 07, 2015 at 02:46 UTC ( [id://1144015]=note: print w/replies, xml ) Need Help??


in reply to Re^2: duplicating the rows and joining three lines into one using perl script
in thread duplicating the rows and joining three lines into one using perl script

Thank you for using code tags. There is absolutely no need to bring Excel into this process. If I understand correctly, here's what you want to do (in pseudo-code):
while ( reading from the input file yields a line of data ) { if ( the line begins with "RS," ) { replace the newline character(s) at the end of the line with a c +omma save this line in a variable called $line_out } elsif ( the line begins with "RAd," ) { replace the final newline character(s) with a comma (just like a +bove) append this line to $line_out } elsif ( the line begins with "RC," ) { print $line_out with the current line appended to it. } }
Rendering that in actual perl syntax is pretty simple, and will be less verbose than what I've shown. Let us know if you have a problem with that step.

UPDATE: I added a more specific condition to get into the third block (for printing output), and I added commas to the regex-match conditions for all three blocks, just to be "safe".

Another update: I didn't intend to be obtuse - here's what I meant in actual perl code:

use strict; use warnings; my $line_out; open ( MOV, "<", "test.cxv" ) or die "test.csv: $!\n"; while (<MOV>) { if ( /^RS,/ ) { s/\s+$/,/; $line_out = $_; } elsif ( /^RAd,/ ) { s/\s+$/,/; $line_out .= $_; } elsif ( /^RC,/ ) { print $line_out . $_; } }

Replies are listed 'Best First'.
Re^4: duplicating the rows and joining three lines into one using perl script
by Laurent_R (Canon) on Oct 07, 2015 at 08:06 UTC
    Yes, it is a very simple problem, indeed, with a straight forward solution.

    Perhaps just a minor point: a comma probably needs to be added between the RS-RAD and RAD-RC concatenated lines.

      I was taking for granted, based on the OP examples, that the input would always be one RS line followed by one RAd line followed by one ore more RC lines. If that's true, then "$line_out" always starts fresh on each RS line, and accumulates content until the first RC line, at which point the current RC line is only appended in the print statement, and not permanently appended to $line_out.
        Yes, and that's also how I understood it. My only point was only about adding a comma to separate the line fragments, but I had overlooked that you're actually adding the comma it in the:
        s/\s+$/,/;
        code lines.

        Sorry about the useless comment.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found