in reply to perl 1-liner to join a list?

Perhaps you think
push @a;
does something? Maybe you wanted
push @a, $_;

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: perl 1-liner to join a list?
by bfdi533 (Friar) on Nov 11, 2003 at 21:27 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;

        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.

        But print takes a list of things too (and prints them, joined by $,) yet uses $_ as the default, so that doesn't work as a distinguishing feature.

        Smylers

Re: •Re: perl 1-liner to join a list?
by bfdi533 (Friar) on Nov 11, 2003 at 21:29 UTC

    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

        That's OS dependent. On Win2K I get the error "> was unexpected at this time." using the switched quotes as you provided. (But perl -le "print join ',',map{chomp;$_}<>" file works just fine)
Re: &bull; perl 1-liner to join a list?
by Roy Johnson (Monsignor) on Nov 12, 2003 at 16:35 UTC
    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?