Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Perl Boolean Evaluation Shows Incorrect Results

by Tanktalus (Canon)
on Mar 01, 2016 at 00:24 UTC ( [id://1156496]=note: print w/replies, xml ) Need Help??


in reply to Perl Boolean Evaluation Shows Incorrect Results

You're wisely printing out the condition you're about to evaluate right before doing so ... but did you read the output?

$cTestH{$dbFile}{$timeRng}{$regionName}{$1} - see that $1 there? You escaped the $, so $1 shows up literally. Not what you want. Not sure what you want, but either that \$1 should become $1 or \$$1 - look at the output closely for both of those to see how they turn up and pick the right one for your purposes.

What I've done in the past is to just replace it with the new value directly, which means getting rid of all them leaning toothpicks: $bool =~ s/([a-zA-Z]\w*)/$cTestH{$dbFile}{$timeRng}{$regionName}{$1}/g; - you end up with much less to evaluate. However, you don't get any short-circuiting if any of the lookups require significant time (e.g., it's a tied hash or you're doing some sort of db lookup or whatever).

Replies are listed 'Best First'.
Re^2: Perl Boolean Evaluation Shows Incorrect Results
by Athanasius (Archbishop) on Mar 01, 2016 at 02:59 UTC
    getting rid of all them leaning toothpicks

    Another way to eliminate the leaning toothpicks is to single-quote the non-interpolating part of the replacement string before making the replacement:

    my $rep = '$cTestH{$dbFile}{$timeRng}{$regionName}{'; my $bool = 'A&!B&!C'; $bool =~ s/([a-zA-Z]\w*)/$rep$1}/g;

    Output:

    12:55 >perl 1560_SoPW.pl Evaluating Test Condition: $cTestH{$dbFile}{$timeRng}{$regionName}{A}& +!$cTestH{$dbFile}{$timeRng}{$regionName}{B}&!$cTestH{$dbFile}{$timeRn +g}{$regionName}{C}... Result is: 1 12:55 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-25 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found