Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Tidy up conditions

by martin (Friar)
on Mar 18, 2015 at 23:49 UTC ( [id://1120520]=note: print w/replies, xml ) Need Help??


in reply to Tidy up conditions

You could use the short-cut behaviour of boolean operators like this:
$level = $access{$item} || $item =~ /^([^:]+):/ && $access{"$1:*"} || $access{'*'} || 0;
Note that this assumes your %access hash does not contain boolean false values, as your initial solution did. To actually check for the presence of these keys in the hash, the exists function will help.

Replies are listed 'Best First'.
Re^2: Tidy up conditions
by tel2 (Pilgrim) on Mar 19, 2015 at 02:01 UTC
    Thanks heaps for that, Martin!  Awesome answer!  Concise, yet...functional.

    I don't understand your concern about boolean false values, though. Could you give me an example which would make your code fail, please?

    Thanks again.
    Tel2

      my %hash = (0=>'that was false', ''=>'oops false too', 42=>'So very true');

      The first two keys there evaluate to false, and would cause a problem with the code.

        OK - thanks SJ,

        I had thought that when Martin said "boolean false values", he literally meant hash values, as opposed to hash keys, which is why I questioned it, but I guess he meant values in the more general sense (i.e. keys in this case).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-24 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found