Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How can I make it to a one liner

by greatshots (Pilgrim)
on Oct 08, 2007 at 06:14 UTC ( [id://643395]=perlquestion: print w/replies, xml ) Need Help??

greatshots has asked for the wisdom of the Perl Monks concerning the following question:

monks,

#!/usr/bin/perl use strict; while ( <DATA> ) { chomp; my $line = $_; my $i = 0; foreach my $val ( split(',',$line) ) { if ( $i == 2 ) { $i = 0; print "$val\n"; next; } print "$val,"; $i ++; } } __DATA__ userid,application_id,time,userid,application_id,time,userid,applicati +on_id,time ,
How it can be in one-liner ?

Replies are listed 'Best First'.
Re: How can I make it to a one liner
by moritz (Cardinal) on Oct 08, 2007 at 07:09 UTC
    Try this one:

    perl -ne 'chomp;for(split /,/){print ++$i%3 ? "$_," : "$_\n" }'

      There are some corner cases where the different solutions disagree.

      The test file:

      a,b,c,d,e f,g,e,h i,j, l,m n ,

      greatshots' output:

      a,b,c d,e,f,g,e h,i,j,l,m,n,
      (no newline at the end)

      My output:

      a,b,c d,e,f g,e,h i,j,l m,n,
      (no trailing newline)

      ikegami's output:

      a,b,c d,e f,g e,h i,j l,m n ,

      It would be interesting to know what the desired output is (and if it matters at all)

Re: How can I make it to a one liner
by ikegami (Patriarch) on Oct 08, 2007 at 07:19 UTC

    Treat "," as the end of line marker (-054).
    Every third line ($. % 3 == 0),
    change the trailing comma to a newline (s/,/\n/).
    Print the result (-p).

    perl -054pe"$.%3||s/,/\n/"

    In action:

    >echo a,b,c,d,e,f,g,h,i | perl -054pe"$.%3||s/,/\n/" a,b,c d,e,f g,h,i

    Update: As moritz points out, I assumed all the input is on one line.

Re: How can I make it to a one liner
by jeanluca (Deacon) on Oct 08, 2007 at 07:23 UTC
    Be careful with those one-liners, they will make your code hard to understand (e.g. the impressive solution from moritz proves my point)
    Anyway, I would go for a more readable solution, something like
    while ( <DATA> ) { chomp; my @line = ($_ =~ /\w+,\w+,\w+/g) ; printf "%s\n", join("\n",@line); }
    Cheers
    LuCa
Re: How can I make it to a one liner
by jwkrahn (Abbot) on Oct 08, 2007 at 09:52 UTC
    perl -F, -lane'print join ",", @x while @x = splice @F, 0, 3'
Re: How can I make it to a one liner
by Somni (Friar) on Oct 08, 2007 at 07:39 UTC
    Another alternative, with the advantage of dealing well with a short line (one without three comma-separated values).
    #!/usr/bin/perl use warnings; use strict; while (<DATA>) { chomp; my @line = split /,/; while (my @excerpt = splice(@line, 0, 3)) { print join(",", @excerpt), "\n"; } } __DATA__ userid,application_id,time,userid,application_id,time,userid,applicati +on_id,tim userid,application ,
Re: How can I make it to a one liner
by Punitha (Priest) on Oct 08, 2007 at 07:15 UTC

    Hi greatshots,

    This may be useful for you

    while (my $line = <DATA>){ my $i=0; chomp($line); my @array = split(',',$line); while($i<=$#array){ print "$array[$i+2]\n"; $i++; } }

    Punitha

Re: How can I make it to a one liner
by educated_foo (Vicar) on Oct 08, 2007 at 18:16 UTC
    perl -pe 'chomp if 2../^__DATA__$/'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (1)
As of 2024-04-19 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found