Why chop fails?

Discussion in 'Perl Misc' started by Huey, Dec 24, 2003.

  1. Huey

    Huey Guest

    Hi All,

    Merry Xmas! I wonder somebody would work at this moment, but this
    simple problem just drives me nuts! How can a chop function fail? I
    simply read from a text file, and need to clean up the newline char
    with chop, but failed! My simply code:

    #!perl
    # records in file foo.txt is like "me:123\n"
    open (F, "./foo.txt");
    while (<F>) {
    ($u, $p) = split(/:/, $_);
    chop $p;
    if ( $p eq "123" ) {
    print "got ya!", "\n";
    }
    }
    close(F);

    I am using cygwin perl. I noticed $p looked correct in command-line
    when printed. The problem is the if () test never go true, it means
    that $p never equals "123", and chop never worked! Aha! Funny? Anybody
    ever experienced such a thing? Please give me your solution, thanks a
    lot!

    Huey
    Huey, Dec 24, 2003
    #1
    1. Advertising

  2. Huey wrote:
    > [...] How can a chop function fail? I
    > simply read from a text file, and need to clean up the newline char
    > with chop, but failed!


    Why are you using chop() instead of chomp()?

    > My simply code:
    >
    > #!perl
    > # records in file foo.txt is like "me:123\n"
    > open (F, "./foo.txt");
    > while (<F>) {


    The usual method is to chomp() first, then process the line further. You are
    doing it the other way round which is not wrong, but counterintuitive.

    > ($u, $p) = split(/:/, $_);
    > chop $p;
    > if ( $p eq "123" ) {
    > print "got ya!", "\n";
    > }
    > }
    > close(F);
    >
    > I am using cygwin perl.


    That implies you are running on Windows which uses a two-character
    end-of-line indicator. chop() will remove only the last character (I think
    that's the \0x0D), still leaving the other character intact (the \0x0A)

    > I noticed $p looked correct in command-line
    > when printed. The problem is the if () test never go true, it means
    > that $p never equals "123",


    Well, of course it doesn't because $p has the value "123\0x0A", i.e. it
    contains 4 characters while you are comparing it with a text of 3
    characters. That ain't gonna match.

    > and chop never worked!


    How do you know that? You didn't compare the string length before and after
    the chop()?
    I bet chop() worked perfectly fine and did remove the last character from
    the string, just as it is supposed to do.

    > [...] Please give me your solution,


    Use the right function for the purpose: chomp()
    Or are you still programming in Perl4?

    jue
    Jürgen Exner, Dec 24, 2003
    #2
    1. Advertising

  3. Huey

    Nick Santos Guest

    "Huey" <> wrote in message
    news:...
    > Hi All,
    >
    > Merry Xmas! I wonder somebody would work at this moment, but this
    > simple problem just drives me nuts! How can a chop function fail? I
    > simply read from a text file, and need to clean up the newline char
    > with chop, but failed! My simply code:
    >
    > #!perl
    > # records in file foo.txt is like "me:123\n"
    > open (F, "./foo.txt");
    > while (<F>) {
    > ($u, $p) = split(/:/, $_);
    > chop $p;
    > if ( $p eq "123" ) {
    > print "got ya!", "\n";
    > }
    > }
    > close(F);
    >
    > I am using cygwin perl. I noticed $p looked correct in command-line
    > when printed. The problem is the if () test never go true, it means
    > that $p never equals "123", and chop never worked! Aha! Funny? Anybody
    > ever experienced such a thing? Please give me your solution, thanks a
    > lot!
    >
    > Huey


    I agree. Always use chomp for newline, because it will only delete the
    newline character and then if for some odd reason you don't actually have a
    newline, you aren't deleting necessary parts of your string.
    Nick Santos, Dec 24, 2003
    #3
  4. Huey wrote:
    >
    > Merry Xmas! I wonder somebody would work at this moment, but this
    > simple problem just drives me nuts! How can a chop function fail? I
    > simply read from a text file, and need to clean up the newline char
    > with chop, but failed! My simply code:
    >
    > #!perl
    > # records in file foo.txt is like "me:123\n"
    > open (F, "./foo.txt");


    You should _ALWAYS_ verify that the file opened correctly.

    open F, 'foo.txt' or die "Cannot open 'foo.txt' $!";


    > while (<F>) {
    > ($u, $p) = split(/:/, $_);
    > chop $p;


    You should use chomp() instead of chop().

    chomp;
    ( $u, $p ) = split/:/;


    > if ( $p eq "123" ) {


    You should use a numerical comparison operator to compare numbers.

    if ( $p == 123 ) {


    > print "got ya!", "\n";
    > }
    > }
    > close(F);




    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Dec 24, 2003
    #4
  5. Huey

    Jay Tilton Guest

    (Huey) wrote:

    : #!perl
    : # records in file foo.txt is like "me:123\n"
    : open (F, "./foo.txt");
    : while (<F>) {
    : ($u, $p) = split(/:/, $_);
    : chop $p;
    : if ( $p eq "123" ) {
    : print "got ya!", "\n";
    : }
    : }
    : close(F);
    :
    : I am using cygwin perl. I noticed $p looked correct in command-line
    : when printed. The problem is the if () test never go true, it means
    : that $p never equals "123", and chop never worked!

    chop() might not be doing what you expect, but that doesn't mean it's not
    doing what it's supposed to do.

    Use of the preferred chomp() function to remove a record separator has
    already been covered in this thread. This is more about determining
    whether the fault lies in the function or in the programmer's assumption on
    what the function should be doing.

    As a strictly temporary measure to convince yourself chop() is really doing
    something, you could wrap the function in a subroutine of your own that
    emits a message on what's going on:

    use subs 'chop';
    sub chop {
    chop($_) unless @_; # act on $_ if no arguments passed
    my $c;
    printf STDERR
    "chop:length before=%d; removed chr(%vd); length after=%d\n",
    length, $c = CORE::chop, length
    for @_;
    $c;
    }

    my $foo = "me:123\n";
    chop $foo;
    # prints "chop:length before=7; removed chr(10); length after=6"
    Jay Tilton, Dec 25, 2003
    #5
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,739
    Smokey Grindel
    Dec 2, 2006
  3. Johnathan Smith

    chop and chop!

    Johnathan Smith, Jan 8, 2008, in forum: Ruby
    Replies:
    2
    Views:
    123
    darren kirby
    Jan 8, 2008
  4. yusufm

    print chop; VS chop; print;

    yusufm, Mar 9, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    108
    Tad McClellan
    Mar 9, 2006
  5. martin
    Replies:
    3
    Views:
    164
    Joe Smith
    Apr 15, 2006
Loading...

Share This Page