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
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
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 ??
| [reply] [d/l] [select] |
|
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
| [reply] [d/l] [select] |
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
| [reply] [d/l] |
|
perl -le 'print join ",",map{chomp;$_}<>' file
-enlil | [reply] [d/l] |
|
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)
| [reply] [d/l] |
|
|
|
Re: • 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 • 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? | [reply] |