Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: testing an if statement in a string

by Laurent_R (Canon)
on Oct 31, 2013 at 15:21 UTC ( [id://1060598]=note: print w/replies, xml ) Need Help??


in reply to testing an if statement in a string

Do you really need to store your conditions in a string? If the context permits it, you may as well store it in a code reference, as shown in the following session under the debugger:

DB<1> @field = qw/0 0 0 0 0 0 PM 0 CAVENDI/; DB<2> $code_ref = sub {$field[shift] eq 'PM' && $field[shift] eq ' +CAVENDI'}; DB<3> $ignore_flag = $code_ref->(6, 8) DB<4> p $ignore_flag 1 DB<5> $ignore_flag = $code_ref->(3, 8) DB<6> p $ignore_flag DB<7> print "true" if $code_ref->(6, 8) true DB<8> print "true" if $code_ref->(3, 8) DB<9>

Here, I have added a feature (dynamic passing of the array subscripts as parameters to the coderef), but the subscripts might as well be hard coded as in your original example.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 07:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found