Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Searching between two hashes

by AnomalousMonk (Chancellor)
on Nov 10, 2017 at 20:50 UTC ( #1203136=note: print w/replies, xml ) Need Help??

in reply to Searching between two hashes

if($ID = $ID2 and $mac2 = ''){ $$ref2{'Network ID 1'} = $mac; }else{ push @extras,\$ref; }

Are you aware that the expressions  $ID = $ID2 and  $mac2 = '' are assignments and not comparisons? In any case, the  $mac2 = '' assignment will always be boolean false and the if-conditional will always be false. Probably what you meant was
    if ($ID == $ID2 and $mac2 eq '') { ... } else { ... }
because  $mac2 is certainly being compared to a string, and I suspect that  $ID == $ID2 is likewise a string comparison and therefore should be  $ID eq $ID2 instead.

Update: An example:

c:\@Work\Perl\monks>perl -wMstrict -le "my ($ID, $ID2, $mac2) = ('foo', 'foo', ''); ;; if($ID eq $ID2 and $mac2 eq ''){ print 'true block'; }else{ print 'false block'; } " true block c:\@Work\Perl\monks>perl -wMstrict -le "my ($ID, $ID2, $mac2) = ('foo', 'foo', ''); ;; if($ID = $ID2 and $mac2 = ''){ print 'true block'; }else{ print 'false block'; } " false block

Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Searching between two hashes
by TStanley (Canon) on Nov 10, 2017 at 21:03 UTC
    Thanks for that. After 17 years of programming with Perl, I still get those particular operators confused. Part of it is also the fact that I don't do a whole lot of programming any more.

    People sleep peaceably in their beds at night only because rough men stand ready to do violence on their behalf. -- George Orwell
      I still get those particular operators confused.

      Top tip: since you know that you fall victim to this (which is more than half the battle), don't write them as $mac2 = '' which is plausible, but instead like '' = $mac2 which is not. The latter is trapped at compile time. Then when you see the compiler errors you can replace the offending expression with the correct '' eq $mac2. Always put the literal value on the left side of the operator to prevent it being a plausible assignment. HTH.

      (Edited for typo)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1203136]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2018-07-17 12:18 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (363 votes). Check out past polls.