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

Re: Concatenate every 5 lines of input with a comma.

by linuxer (Curate)
on Jul 09, 2009 at 19:24 UTC ( [id://778671]=note: print w/replies, xml ) Need Help??


in reply to Concatenate every 5 lines of input with a comma.

Did you already check perlvar?

Look for $. and $\.

But I wonder how you produce values separated with ; (semicolon), when you used , (comma) in your code? ;))

Update: example added:

#!/usr/bin/perl # vi:ts=4 sw=4 et: use strict; use warnings; while ( <DATA> ) { chomp; local $\ = $. % 5 ? ";" : "\n"; print; } __DATA__ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Replies are listed 'Best First'.
Re^2: Concatenate every 5 lines of input with a comma.
by ikegami (Patriarch) on Jul 09, 2009 at 19:34 UTC
    As a one-liner:
    perl -ple'$\ = $.%5 ? ";" : $/' infile > outfile

    In-place:

    perl -i.bak -ple'$\ = $.%5 ? ";" : $/' file

    If it wasn't a one-liner, I wouldn't use $\

    while ( <DATA> ) { chomp; print $_, $.%5 ? ";" : $/; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-03-28 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found