•Re: perl 1-liner to join a list?
by merlyn (Sage) on Nov 11, 2003 at 21:16 UTC
|
push @a;
does something? Maybe you wanted
push @a, $_;
| [reply] [d/l] [select] |
|
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] |
|
|
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] |
|
|
|
|
|
| [reply] |
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.";
| [reply] [d/l] [select] |
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
| [reply] [d/l] [select] |
Re: perl 1-liner to join a list?
by jmcnamara (Monsignor) on Nov 11, 2003 at 22:06 UTC
|
perl -lpe '$\=(eof)?$/:","' file
Update: or this
perl -l54pe '$\=$/if+eof' file
--
John.
| [reply] [d/l] [select] |
|
perl -0pe 's/\n(?!$)/,/g'
:-)
-sauoq
"My two cents aren't worth a dime.";
| [reply] [d/l] [select] |
|
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/,/'
| [reply] [d/l] |
|
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
| [reply] [d/l] |
Re: perl 1-liner to join a list?
by diotalevi (Canon) on Nov 11, 2003 at 21:29 UTC
|
| [reply] [d/l] |
|
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.";
| [reply] [d/l] |
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";' | [reply] |