Why chop fails?

H

Huey

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
 
J

Jürgen Exner

Huey said:
[...] 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
 
N

Nick Santos

Huey said:
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.
 
J

John W. Krahn

Huey said:
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
 
J

Jay Tilton

(e-mail address removed) (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"
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top