Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Check on if statement

by Anonymous Monk
on Aug 17, 2002 at 16:52 UTC ( [id://190885]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi.

I've got the following if statement: if($foo !~ /(|\?)/)

It's not working the way I need it to, though. What I'm trying to do is basically get the system to check if $foo is either a 'blank' (empty) variable or if it's value is just a question mark ('?').

Now, how can I represent that empty/blank value in the above statement?

Thanks,
Ralph.

Replies are listed 'Best First'.
Re: Check on if statement
by tommyw (Hermit) on Aug 17, 2002 at 16:57 UTC

    Even without the problem of recognising an empty string, since you're using a regular expression, it will recognise any string which contains a question mark (even if it's got other characters in the string).

    what's wrong with brute force?

    if ($foo eq '' || $foo eq '?') { do something; }

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.

      Interesting ... "what's wrong with brute force?" Would two "eq" actually be more efficient then one "=~" ? It's probably splitting hairs, but it got me thinking .... Carl

        Well, my first thought was simply to write something that was simple, and not to worry about the speed. It depends on how many tens of million times this is going to happen before the speed even becomes an issue.

        However, since you asked:

        use Benchmark; $a="Hello"; timethese(10_000_000, { eq => sub { return ($a eq "" || $a eq "?") }, regexp => sub { $a=~/^\??$/ } } );
        gives
        Benchmark: timing 10000000 iterations of eq, regexp... eq: 7 wallclock secs ( 3.51 usr + -0.01 sys = 3.50 CPU) @ 28 +57142.86/s (n=10000000) regexp: 22 wallclock secs (10.15 usr + 0.13 sys = 10.28 CPU) @ 97 +2762.65/s (n=10000000)
        The figures don't change (significantly) when $a="?". When it's an empty string, the equality takes half the time (since the second equality is never used), and the regexp is slightly slower (because it has to backtrack). undef makes both slightly slower.

        --
        Tommy
        Too stupid to live.
        Too stubborn to die.

        In this case it would be more efficient because the AM who posted wants to check for either no value or a single question mark, if he/she wanted to check to see if the string was empty or a question mark anywhere in $foo then you would probably need something like
        if(!$foo || $foo =~ /\?/){ # either its blank or there is a ? somewhere in it }
Re: Check on if statement
by vek (Prior) on Aug 17, 2002 at 16:56 UTC
    Try this:
    if (! defined $foo || $foo =~ /\?/)

    Update: typo, should have been ! defined...

    -- vek --

      I don't think defined() is gonna work. That will be true if $foo has any value and will short-circuit the regex....


      What's this about a "crooked mitre"? I'm good at woodwork!

        I think defined() is a good idea if there's a possibility of $foo being undefined, so as to avoid a warning about an undef being used in a string comparison/regex. Assuming an undefined value counts as a blank, I would write it like this:

        if (!defined($foo) or $foo =~ m/^\??$/) { # Foo is valid }

        Thus: "If foo is undefined, or matches an empty string or a single question mark."

        But wasn't there something about \n's counting as "^" and/or "$"? Hmmm...


        The Secret to Fortune Cookies in One Line
        print join("... in bed", `fortune fortunes` =~ m/^(.*)(\.|\?|\!)$/), "\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found