Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Default Hash Key

by tford (Beadle)
on May 02, 2008 at 04:32 UTC ( #684094=note: print w/ replies, xml ) Need Help??


in reply to Re: Default Hash Key
in thread Default Hash Key

Wow thanks for the reply okol (and for the benefit of the doubt!) I really learned a lot from all the replies, but yours is nice and simple and probably about as fast as possible.

I tried just a slight variation and I think it's going to work nicely.

use strict; use warnings; my %description = ( a => 'a vowel', b => 'a consonant', default => 'not in the alphabet' ); for my $char (qw/a b c/) { my $d = $description{$char} || $description{default}; print("$char is $d\n"); }
I still can't quite get my head around the way that the logical or works. I would think that it would only return 1 or 0, but evidently here it's returning the whole string back again.

Anyway, thanks for all the help everybody. I did kinda post problem Y without getting into the specifics of problem X, but that was only because I thought X would bore everybody to tears!


Comment on Re^2: Default Hash Key
Download Code
Re^3: Default Hash Key
by mrons (Sexton) on May 02, 2008 at 05:21 UTC
    Just be careful about $description{foo} that evaluate to false:
    use strict; use warnings; my %description = ( a => 'a vowel', b => 'a consonant', 0 => '0', default => 'not in the alphabet' ); for my $char (qw/a b c 0/) { my $d = $description{$char} || $description{default}; print("$char is $d\n"); }
      Which is why god invented the '//' operator -- most definitely a better choice than '||' in this context.


      Evan Carroll
      I hack for the ladies.
      www.EvanCarroll.com
      Which is where the exists used in the first two original replies to the OP comes in handy...

      Because of things like this, I find myself using defined and exists a lot when writing code. It may be more verbose but better safe than sorry... I hope I'm not too much of a Captain Obvious :)
      That problem is avoided in Perl 5.10.0 with my $d = $description{$char} // $description{default}; # check for undefined rather than false As for getting one's head around the logical or, it's really quite simple: $a || $b || $c || ... yields the first value that is true (or false if none are). Perl didn't invent it; it's a common operator in many languages, dating back to Snobol, I think.
Re^3: Default Hash Key
by roboticus (Canon) on May 02, 2008 at 10:42 UTC
    tford:

    perldoc perlop yields:

    C-style Logical Or

    Binary "||" performs a short-circuit logical OR operation. That is, if the left operand is true, the right operand is not even evaluated. Scalar or list context propagates down to the right operand if it is evaluated.

    The "||" and "&&" operators return the last value evaluated (unlike C's "||" and "&&", which return 0 or 1). Thus, a reasonably portable way to find out the home directory might be:

    $home = $ENV{'HOME'} || $ENV{'LOGDIR'} || (getpwuid($<))[7] || die "You're homeless!\n";
    ...roboticus

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2014-07-10 22:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls