print chop; VS chop; print;

Y

yusufm

Why does this work (prints out expected strings):

cat somefile | perl -ne 'chop; print;'

while this just prints out blank lines:

cat somefile | perl -ne 'print chop;'

thanks.
 
P

plalli

yusufm said:
Why does this work (prints out expected strings):

cat somefile | perl -ne 'chop; print;'

while this just prints out blank lines:

cat somefile | perl -ne 'print chop;'

Because that's what it's supposed to do.

Have you read the Perl built-in documentation for the function you're
using?

perldoc -f chop

chop does not return the chop'ed string - it returns the character that
was chopped. In your case, that's the newline character.

Paul Lalli.
 
T

Tad McClellan

yusufm said:
Why does this work (prints out expected strings):

cat somefile | perl -ne 'chop; print;'


Because print() with no arguments prints $_

while this just prints out blank lines:

cat somefile | perl -ne 'print chop;'


print() prints the return value from chop().

What is the return value from chop()?

perldoc -f chop




But you probably should be using chomp() instead, or maybe
even the -l command line switch...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top