Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Concatenate every 5 lines of input with a comma.

by jsh (Initiate)
on Jul 09, 2009 at 19:17 UTC ( #778667=perlquestion: print w/replies, xml ) Need Help??
jsh has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys. i have situation that i need to have below kind manipulation done by perl.
awk 'ORS=NR%5?",":"\n"'
i cannot figure it out. I need to have output like below, print 5 values comma separated and new line.
val1;val2;val3;val4;val5 val1;val2;val3;val4;val5 val1;val2;val3;val4;val5 etc...

Replies are listed 'Best First'.
Re: Concatenate every 5 lines of input with a comma.
by linuxer (Curate) on Jul 09, 2009 at 19:24 UTC

    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
      As a one-liner:
      perl -ple'$\ = $.%5 ? ";" : $/' infile > outfile


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

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

      while ( <DATA> ) { chomp; print $_, $.%5 ? ";" : $/; }
Re: Concatenate every 5 lines of input with a comma.
by SuicideJunkie (Vicar) on Jul 09, 2009 at 19:28 UTC
    That's not a comma separating them in your example...
    But regardless of the sample data, what have you tried?
    Have you tried printing five values and then a newline, and what did you have trouble with during that process?

    It sounds like you've distilled a decent specification, although there is still the question of where the values are coming from, and what you need to do about values that do not come in multiples of five.
    If you're reading whole lines from somewhere such as a file or STDIN, make sure you chomp() off the newline before printing them.

    The monks can fry your fish, and they can give you some tips and some bait, but you still need to wake up and climb onto the boat.
      Hi, sorry my explanation was bad. Comma or semi-colon does not matter :) My scripts seeks serial numbers through ssh connection, return value comes from foreach loop.
      round 1; 123456 round 2; 123456 etc.. and i need to have output print like i said. 12345;12345;12345 12345;12345;12345 etc.. Thanks.
        Hi, awesome guys, answer was above
        while ( <DATA> ) { chomp; print $_, $.%5 ? ";" : $/; }
Re: Concatenate every 5 lines of input with a comma.
by ForgotPasswordAgain (Curate) on Jul 09, 2009 at 19:24 UTC

    UPDATE: Oh, sorry, I'd only read the subject. I don't understand the body of your post.


    perl -pe's/$/,/ if not$.%5' file

      Not even close:

      $ seq 20 | perl -pe's/$/,/ if not$.%5' 1 2 3 4 5, 6 7 8 9 10, 11 12 13 14 15, 16 17 18 19 20,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://778667]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2018-07-17 21:41 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (378 votes). Check out past polls.