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

Discussion in 'Perl Misc' started by Tim McDaniel, Aug 27, 2012.

  1. Tim McDaniel

    Tim McDaniel Guest

    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,
     
    Tim McDaniel, Aug 27, 2012
    #1
    1. Advertising

  2. Tim McDaniel

    Tim McDaniel Guest

    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,
     
    Tim McDaniel, Aug 27, 2012
    #2
    1. Advertising

  3. Tim McDaniel

    Tim McDaniel Guest

    In article <>,
    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,
     
    Tim McDaniel, Aug 27, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. korean_dave
    Replies:
    0
    Views:
    276
    korean_dave
    Jul 24, 2008
  2. rp
    Replies:
    1
    Views:
    537
    red floyd
    Nov 10, 2011
  3. Joshua Muheim
    Replies:
    8
    Views:
    196
    Dejan Dimic
    Nov 23, 2007
  4. hymie!
    Replies:
    1
    Views:
    360
    Tim McDaniel
    Aug 27, 2012
  5. Replies:
    7
    Views:
    108
    Wolfgang Maier
    Mar 27, 2013
Loading...

Share This Page