Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

perl 1-liner to join a list?

by bfdi533 (Friar)
on Nov 11, 2003 at 21:15 UTC ( [id://306364]=perlquestion: print w/replies, xml ) Need Help??

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

I am looking for a perl 1-liner to join a list of items from stdin to stdout joined with a comma. I have searched the monastary and cannot find anything on this.

Say my list looks like this and is in a file called 'file':

1 2 3 4 5 6 7 8 9 10

and I want it to look like this:

1,2,3,4,5,6,7,8,9,10
I have tried:
cat file | perl -e "while (<>) { push @a; } ; print join(',',@a);"

This does not produce any output at all.

I must be missing something simple here.

Ed

Replies are listed 'Best First'.
•Re: perl 1-liner to join a list?
by merlyn (Sage) on Nov 11, 2003 at 21:16 UTC

      Well, yes, actually I did. I thought that

      push @a;

      was equivalent to

      push @a, $_;

      in the same manner that

      print;

      is equivalent to

      print $_;

      Maybe I need to brush up on when $_ has to be used and when i can be omitted.

      Is there a good ref material or camel chapter on this?

      Ed

        perldoc -f push doesn't say anything about defaulting to $_. Also, looking up push in the Camel doesn't show the $_ box like it does for print (which is, conveniently, on the facing page in my Camel 3rd ed.)

        But I'm not criticizing, I might have made the same assumptions.

        This is how I try to remember this.  push takes a list as argument and hence  $_ cannot be taken as a default argument for this.

        Update But then as Smylers pointed out  print also takes a list of arguments but still takes $_ as default argument when there is none. puzzling ??

        -T

        use perl; use strict;

      But, having said that, I have tried my code as you suggest and still get no results.

      cat file | perl -e "while (<>) { push @a, $_; } print join(',',@a);"

      Ed

        Try switching the double quotes for single quotes, and the single quotes for double quotes.

        Here is yet another way to do it:

        perl -le 'print join ",",map{chomp;$_}<>' file

        -enlil

      Is there a reason that $_ shouldn't be a default argument for push? That is, would it be a Bad Idea for future versions?

      Also, do you find that the &bull; in your titles renders a bullet? It doesn't for me, though it does produce a bullet when used in the body. Why do you do it?

Re: perl 1-liner to join a list?
by sauoq (Abbot) on Nov 11, 2003 at 21:27 UTC
    perl -le 'my @a = <>; chomp @a; print join ",", @a' file

    BTW, That's a useless use of cat.

    Update: Here's an alternative to avoid reading the whole file into memory:

    perl -e '{ $_=<>; chomp; print; last if eof(); print ","; redo }; prin +t $/'

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: perl 1-liner to join a list?
by Roy Johnson (Monsignor) on Nov 11, 2003 at 21:39 UTC
    perl -e "chomp(@F=<>); print join',',@F" file
    ?
    Or if you're not worried about possibly having a comma after the last line:
    perl -pe "tr/\n/,/" file
    - or do it all with command line args and have no program:
    perl -l0054pe '' file
    If you don't want a trailing comma:
    perl -pe "eof or tr/\n/,/" file
Re: perl 1-liner to join a list?
by jmcnamara (Monsignor) on Nov 11, 2003 at 22:06 UTC

    Another way:
    perl -lpe '$\=(eof)?$/:","' file

    Update: or this

    perl -l54pe '$\=$/if+eof' file

    --
    John.

      You've still got me by one character... perl -0pe 'y/\n/,/;s/,$/\n/'

      Update: But, wait!

      perl -0pe 's/\n(?!$)/,/g'
      :-)

      -sauoq
      "My two cents aren't worth a dime.";
      
        This was a gimme from my earlier post. It's also fairly readable. Compared to yours, the expression is 1 char shorter, and there's one fewer command-line option.
        perl -pe 'eof||y/\n/,/'

        So it's golf, is it? :-)

        perl -lpe '$\=!eof&&","'

        Update: Uh, hmm, just realized that doesn't quite do the same thing (gets rid of the trailing newline). Bah, details.

        -- Mike

        --
        XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
        -- grantm, perldoc XML::Simpler

Re: perl 1-liner to join a list?
by diotalevi (Canon) on Nov 11, 2003 at 21:29 UTC
    perl -0777 -le '$, = ","; print split /\n/, <>' file

    Update: Added -0777 and split /\n/ per sauoq's comment

      He doesn't want output like:

      foo ,bar ,baz

      That -l doesn't chomp unless it is used with -n or -p. I made the same mistake and quickly corrected it.

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: perl 1-liner to join a list?
by dbameister (Initiate) on Nov 12, 2003 at 21:07 UTC
    If you don't mind an extra comma at the end, try this: cat file | perl -e 'while (<>) {chomp;print $_,",";}; print "\n";'

Log In?
Username:
Password:

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

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

    No recent polls found