Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Whatever happened to chomp?

by jsprat (Curate)
on Dec 14, 2003 at 14:57 UTC ( #314639=note: print w/replies, xml ) Need Help??


in reply to Whatever happened to chomp?

2 is a number, and perl is trying to DWYM with the numerical compare (==). Try putting
one two three four

in __DATA__, and

change the conditional to if ($data eq '2'). Now see what effect chomp has...

Update: Struck out the data section and added bit about numerical compare to the first sentence. I shouldn't have included the red herring (one, two, three). The original point was supposed to be that perl will treat a variable like a number in numerical context (== instead of eq).

Replies are listed 'Best First'.
Re: Re: Whatever happened to chomp?
by pg (Canon) on Dec 14, 2003 at 19:53 UTC
    2 is a number.

    No, 2 could be a number or a string base on the context as how you use it. A better answer is to point out what he really misunderstood: the context.

    It is pretty straight to simply change == to eq, and quot 2:

    use Tk; use Tk::NoteBook; use strict; use warnings; use strict; use warnings; while (my $data = <DATA>) { chomp $data; print $data; if ($data eq "2"){print "->got 2\n"} } print "Enter a 2\n"; my $input = <>; chomp $input; if ($input eq "2"){print "->got 2\n"} __DATA__ 1 2 3 4

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://314639]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2020-09-22 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (128 votes). Check out past polls.

    Notices?