Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Merge pairs of lines

by integral (Hermit)
on Jan 29, 2003 at 18:25 UTC ( [id://231045]=CUFP: print w/replies, xml ) Need Help??

Ignoring blank lines, pairs of lines are merged together (with a comma separating them).

Example:

a
b

c
d

Becomes:

a,b
c,d
perl -pe 'next unless /\S/; s/\n/,/ if ($a = 1 - $a)'

Replies are listed 'Best First'.
Re: Merge pairs of lines
by Aristotle (Chancellor) on Jan 31, 2003 at 05:09 UTC
    Using next that way doesn't play with -p unfortunately. Ignoring that issue, here's a doozy to do the same:
    perl -l054p012e'($\,$a)=($a||$\,$b||$/);$b=$\'

    :-)

    Update:

    $ perl -l054p012e'($\,$a)=($a||$\,$b||$/);$b=$\' <<EOT
    > a
    > b
    > c
    > d
    > e
    > f
    > g
    > h
    > EOT
    a,b
    c,d
    e,f
    g,h

    Makeshifts last the longest.

      My attempt,

      perl -pe '$_=/\S/?do{chop;$_.",".<>}:""'
      assumes non-blank lines come in pairs.

      Yours doesn't look right to me though...

      286,0 sauoq@grover:~$ perl -l054p012e'($\,$a)=($a||$\,$b||$/);$b=$\' < + test.txt a,b ,c d,287,0 sauoq@grover:~$ cat test.txt a b c d 288,0 sauoq@grover:~$
      -sauoq
      "My two cents aren't worth a dime.";
      
        It ignores the issues of blank lines, if you mean that, but so does the OP's code because next doesn't work the way he thinks. (The print is in a continue block so it always gets executed short of dieing.) I chose to note and ignore it. What I was trying to do is achieve the described behaviour without explicitly manipulating $_ or printing anything myself.

        Makeshifts last the longest.

Re: Merge pairs of lines
by BrowserUk (Patriarch) on Jan 31, 2003 at 06:39 UTC

    Not for big files

    C:\test>type junk A B C D C:\test>perl -0pe"y/\n//s;s/(.+)\n(.+)/$1,$2/g" junk A,B C,D

    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: Merge pairs of lines
by integral (Hermit) on Jan 31, 2003 at 07:18 UTC
    Now that I've actually tested the code, I have a bugfix for it:
    perl -pe 'goto LINE unless /\S/; s/\n/,/ if ($a = 1 - $a)'
    This fix keeps the structure of the original code, only changing the next to a goto.

    --
    integral, resident of freenode's #perl
    
      Now that I've actually tested the code, I have a bugfix for it:

      You should test before submitting a snippet.

      Even this version has a quirk you should be aware of. It will substitute a comma for a newline on the last line of the file if the input has an odd number of non-blank lines.

      You can use $a ^= 1 as a sufficiently clear alternative to $a = 1 - $a.

      Golfed: perl -pe 'goto LINE if!/\S/;--$|&&s/\n/,/'

      Update: Another several strokes shaved:

      perl -pe '/\S/?--$|&&s/\n/,/:($_="")'

      -sauoq
      "My two cents aren't worth a dime.";
      

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found