Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by linuxer (Deacon)
on Jul 09, 2009 at 19:24 UTC ( #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


Comment on Re: Concatenate every 5 lines of input with a comma.
Select or Download Code
Re^2: Concatenate every 5 lines of input with a comma.
by ikegami (Pope) 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
Node Status?
node history
Node Type: note [id://778671]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-12-28 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls