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

Re^2: var comparison

by nemesisgus (Acolyte)
on Sep 07, 2012 at 16:37 UTC ( #992343=note: print w/ replies, xml ) Need Help??


in reply to Re: var comparison
in thread var comparison

There were no hidden intentions. My question was simply the product of my poor knowledge of Perl :^)

Although it's true that shorter doesn't always mean more efficient, it's also true that when you start with a language you tend to use long and complicated expressions to accomplish tasks that, once you have a little (or no so little) more knowledge, you realize that could have been done in a much shorter (and usually clearer) way.

I was just wondering if this was the case, so the expression:

$var eq 'foo' || $var eq 'bar'

could be translated to something like:

$var eq (foo|bar) (which, of course, does not work)

or similar, the same way the defined-or operator helps to turn $x = $x // 5 into $x //= 5, for instance.

(I understand that the implications of doing $var eq (foo|bar) would probably be more complex).

Consider also that the case I proposed had only two options to compare, so the first version is OK, but with more options a shorter manner would be appreciated. Since that seems not possible, a function would be the best solution as suggested by MidLifeXis.


Comment on Re^2: var comparison
Select or Download Code
Re^3: var comparison
by Anonymous Monk on Sep 08, 2012 at 08:57 UTC

    If your list of possible values gets long, you could store them as keys of a hash and then check for existence or definedness in the hash.

      Yes, that's one of the solutions MidLifeXis proposed and I think it's also considered a Perl "best practice".

        hmm, nobody gave you a code example ?

        DB<100> @option{qw/a b c/}=() => (undef, undef, undef) DB<101> exists $option{a} => 1 #true DB<102> exists $option{d} => "" #false

        but smart-match should be safe as long as you don't forget the quotes.

        Cheers Rolf

Re^3: var comparison
by Marshall (Prior) on Sep 09, 2012 at 19:14 UTC
    I think that $var eq 'foo' || $var eq 'bar' will work very well. If $var is say 'xyz', the string compare will be abandoned at the first letter. "Oh, its not an 'f', lets see if the first letter is an 'b' ... The regex engine is amazing but an expression like this will just call a low level 'C' function that is very efficient.

    My point is that "shorter" source code is not necessary "better" in terms of efficiency or readability.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-09-21 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (175 votes), past polls