print chop; VS chop; print;

Discussion in 'Perl Misc' started by yusufm, Mar 9, 2006.

  1. yusufm

    yusufm Guest

    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.
    yusufm, Mar 9, 2006
    #1
    1. Advertising

  2. yusufm

    Guest

    yusufm wrote:
    > 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.
    , Mar 9, 2006
    #2
    1. Advertising

  3. yusufm <> wrote:
    > 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...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 9, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gusmeister

    Inverse of 'chop @array'

    gusmeister, Mar 6, 2004, in forum: Perl
    Replies:
    1
    Views:
    675
    J├╝rgen Exner
    Mar 6, 2004
  2. Guest
    Replies:
    7
    Views:
    663
    Walter Wang [MSFT]
    Jun 28, 2006
  3. Aaron Walker

    best way to chop off leading char in string?

    Aaron Walker, Nov 22, 2003, in forum: C Programming
    Replies:
    3
    Views:
    435
    Sheldon Simms
    Nov 22, 2003
  4. Johnathan Smith

    chop and chop!

    Johnathan Smith, Jan 8, 2008, in forum: Ruby
    Replies:
    2
    Views:
    123
    darren kirby
    Jan 8, 2008
  5. martin
    Replies:
    3
    Views:
    164
    Joe Smith
    Apr 15, 2006
Loading...

Share This Page