### Re^2: Default Hash Key

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

in reply to Re: 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!

Replies are listed 'Best First'.
Re^3: Default Hash Key
by mrons (Beadle) 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
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 (Chancellor) 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

Create A New User
Node Status?
node history
Node Type: note [id://684094]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2018-07-19 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

Results (398 votes). Check out past polls.

Notices?