Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Re: Neat way of checking that two hash values both exist?

Reply
Thread Tools

Re: Neat way of checking that two hash values both exist?

 
 
Tim McDaniel
Guest
Posts: n/a
 
      08-27-2012
In article <>,
Henry Law <> wrote:
>I'm checking parameters to a little utility I'm writing. It uses
>Getopt::Std, which returns the parameters in a hash %opts.
>
>The logic of the utility requires both flags -h and -p to be specified
>or neither. I'm coding the part that checks whether that is true.
>
>I'm sure there's a neater way than this (which does work, admittedly):
>
>die "Flags -h and -p must be specified together\n"
> if (exists $opts{p} && !exists $opts{h}) || (exists $opts{h} &&
>!exists $opts{p});


if exists $opts{p} == exists $opts{h}

--
Tim McDaniel,
 
Reply With Quote
 
 
 
 
Tim McDaniel
Guest
Posts: n/a
 
      08-27-2012
In article <k1g6rk$ct1$>,
Tim McDaniel <> wrote:
>In article <>,
>Henry Law <> wrote:
>>I'm checking parameters to a little utility I'm writing. It uses
>>Getopt::Std, which returns the parameters in a hash %opts.
>>
>>The logic of the utility requires both flags -h and -p to be specified
>>or neither. I'm coding the part that checks whether that is true.
>>
>>I'm sure there's a neater way than this (which does work, admittedly):
>>
>>die "Flags -h and -p must be specified together\n"
>> if (exists $opts{p} && !exists $opts{h}) || (exists $opts{h} &&
>>!exists $opts{p});

>
> if exists $opts{p} == exists $opts{h}


To expand on that,

IF you have Perl boolean values, by which I mean there are only two
possible values, where one evaluates to true and one evaluates to
false (and I believe that exists fulfils that),

THEN != is the exclusive-or function: it is true if an only if exactly
one of its operands is true.

And therefore == is the inverse: true if and only if both operands are
true or both operands are false.

You may not have such a boolean. For example, you might have a
function returning a number, and you want to just check that both are
zero or both are non-zero. You can't do
somefunc($x) == somefunc($y)
because maybe that's (for example)
12 == 27
which is incorrect.

So !! is Perl's "convert to boolean" operator:
!!(something that evaluates to false) -> ''
!!(something that evaluates to true) -> 1

So the more general way to do exclusive or for values that are simply
either Perl true or Perl false, but might be any true or false value,
!!val1 != !!val2
and so the general "both or neither" is
!!val1 == !!val2

--
Tim McDaniel,
 
Reply With Quote
 
 
 
 
Tim McDaniel
Guest
Posts: n/a
 
      08-27-2012
In article <bvrsg9->,
Ben Morrow <> wrote:
>
>Quoth :
>> >
>> > if exists $opts{p} == exists $opts{h}

>>
>> To expand on that,
>>
>> IF you have Perl boolean values, by which I mean there are only two
>> possible values, where one evaluates to true and one evaluates to
>> false


which was silly. Sorry. For != as exclusive or and similar ==, you
just need two distinct values that can be compared numerically. A
true number and 0 fulfil the condition, but 17 and 42 would work too.
I was just trying to express that you can't have more than two
values.

>> So !! is Perl's "convert to boolean" operator:
>> !!(something that evaluates to false) -> ''
>> !!(something that evaluates to true) -> 1

>
>It's worth being clear that !! is not an operator in its own right, but
>simply two instances of !. I'm sure you know this, but I have seen
>people confused about it in the past.


Quite so. Sorry for being confusing.

>Or, you know, use exclusive or? For some reason I have never been able
>to work out there is no ^^ operator, so you have to use 'xor':


I tend not to remember "and", "or", and "xor" because they came in
well after I started Perling, and there is no ^^. Of course, you need
to remember that "and", "or", and "xor" have very low precedence, so
while you don't need parens in
$both = $a && $b;
$either = !!$a == !!$b
you do need parens in
$both = ($a and $b);
$either = ($a xor $b);

--
Tim McDaniel,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Re: Neat way of checking that two hash values both exist? hymie! Perl Misc 1 08-27-2012 07:34 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Neat way to get rid of [" "] in sys.argv[n:] returns korean_dave Python 0 07-24-2008 08:48 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57