Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

return value of chomp is false?

by babel17 (Acolyte)
on Sep 29, 2009 at 01:23 UTC ( #798031=perlquestion: print w/ replies, xml ) Need Help??
babel17 has asked for the wisdom of the Perl Monks concerning the following question:

Nevermind, total brain fart. was getting the sense of the Trinary wrong

I would have thought that the return value of a successful chomp would be interpreted as "true", but it appears that it returns false. Or maybe I'm just fignerent.

example.
I'm joining a list of newline separated strings from STDIN with '|' so that I can use them as input to a regex.

my $names=join("|",map {chomp $_ ?"":$_} <STDIN>);
but it don't work. To get it to function correctly I have to reverse the sense of the trinary operator.
my $names=join("|",map {chomp $_ ?$_:""} <STDIN>);
Anyone care to enlighten me?

Replies are listed 'Best First'.
Re: return value of chomp is false?
by chromatic (Archbishop) on Sep 29, 2009 at 01:50 UTC

    You're trying to be too clever (edit: in that you likely don't need the return value of chomp):

    my $names = join '|', map { chomp; $_ } <STDIN>;
      That's what I tried first, but that returns (1,$_) for each value of $_

        Not it doesn't. I suspect you used a comma instead of a semi-colon.

        By the way, there's also

        use List::MoreUtils qw( apply ); my $names = join '|', apply { chomp } <STDIN>;
        and
        use Nested::Loops qw( Filter ); my $names = join '|', Filter { chomp } <STDIN>;
Re: return value of chomp is false?
by toolic (Bishop) on Sep 29, 2009 at 01:37 UTC
    I would have thought that the return value of a successful chomp would be interpreted as "true", but it appears that it returns false.
    According to the documentation for chomp:
    It returns the total number of characters removed from all its arguments.
    If there is no newline, for example, it returns 0; if there is a newline, it returns 1.

    So, chomp $_ will return 1, which is true, which causes the ternary operator to return the 'true' result. In your non-working case, it returns the empty string. In your working case, it returns the $_ without the newline.

      But there is always a new line, so it should always be interpreted as true, which means the first case should work, not the second case. ...but that is not the case. The chomp is returning '1' and it's being interpreted as false.
        Let me try to elaborate on my answer.

        Firstly, here is a quote from the documentation for the Conditional Operator:

        Ternary "?:" is the conditional operator, just as in C. It works much like an if-then-else. If the argument before the ? is true, the argument before the : is returned, otherwise the argument after the : is returned.

        Coming from the STDIN handle, I agree that $_ will have a newline character. This means that chomp $_ will return 1. Since Perl treats 1 as a true value, the ternary operator will return the argument before the :.

        In your first case, chomp $_ ?"":$_, the ternary operator returns an empty string.

        In your second case, chomp $_ ?$_:"", the ternary operator returns $_.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2016-06-30 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (405 votes). Check out past polls.