Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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.

Replies are listed 'Best First'.
Re^3: var comparison
by Marshall (Abbot) 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.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992343]
[stevieb]: this link is probably better for an overview of my test software
[stevieb]: the top-level Raspberry Pi distribution, that sucks in all sub modules. All of this software have imminent updated releases coming

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2017-06-25 23:18 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (572 votes). Check out past polls.