problem using evaluations in vars

M

Mike Ballard

Probably didn't word title right but I have these:

$1 ne $3 && $2 ne $4
$1 ne $3 && $2 eq $4
$1 ne $3

and want to do this:

$filter = "\$1 ne \$3 && \$2 ne \$4";

or

$filter = "\$1 ne \$3 && \$2 eq \$4";

etc., depending on argv, right at the start of my code.

I want to define $filter based on argv so that instead of having 3
distinct/explicit "if ()" deeper in my code, I can have 1 "if ($filter)"
loaded with the argv-defined filter.

As I step through the code I can see that the correct filter gets selected
for "if ()" but the problem is it doesn't seem to be doing any evaluating
once there. I've tried all manner of escaping/quoting when defining
$filter but in the "if ()" it always evaluates the same way (and
incorrectly).

Is this some issue with the stmt evaluating only when $filter is defined,
and then later in the "if ()" it's going to remain static? Or is it just
how I'm trying to "protect" it from doing that when assigning it initially
to $filter?

If I'm allowed to do this, what do I have to do to make it work (I looked
at "eval" but gave up kinda quick on it)?

Mike
--
 
J

Joe Smith

Mike said:
Probably didn't word title right but I have these:

$1 ne $3 && $2 ne $4
$1 ne $3 && $2 eq $4
$1 ne $3

@m = $string =~ /(a.*)(b.*)(c.*)(d.*)/ ; Put $1,$2,$3,$4 into array

sub filter1 { $_[1] ne $_[3] and $_[2] ne $_[4]; }
sub filter2 { $_[1] ne $_[3] and $_[2] eq $_[4]; }
sub filter3 { $_[1] ne $_[3]; }
I can have 1 "if ($filter)"

if (filter1(undef,@m)) { ... }
if (filter2(undef,@m)) { ... }
if (filter3(undef,@m)) { ... }

-Joe
 
A

Anno Siegel

Mike Ballard said:
Probably didn't word title right but I have these:

$1 ne $3 && $2 ne $4
$1 ne $3 && $2 eq $4
$1 ne $3

and want to do this:

$filter = "\$1 ne \$3 && \$2 ne \$4";

or

$filter = "\$1 ne \$3 && \$2 eq \$4";

etc., depending on argv, right at the start of my code.

I want to define $filter based on argv so that instead of having 3
distinct/explicit "if ()" deeper in my code, I can have 1 "if ($filter)"
loaded with the argv-defined filter.

As I step through the code I can see that the correct filter gets selected
for "if ()" but the problem is it doesn't seem to be doing any evaluating
once there. I've tried all manner of escaping/quoting when defining
$filter but in the "if ()" it always evaluates the same way (and
incorrectly).

You are entitled to your view, but I'd say the evaluation is correct,
your expectations aren't.
Is this some issue with the stmt evaluating only when $filter is defined,
and then later in the "if ()" it's going to remain static? Or is it just
how I'm trying to "protect" it from doing that when assigning it initially
to $filter?

Exactly. $filter is a string and takes its value when you assign it.
It won't change later just because you have evaluated another pattern
match.
If I'm allowed to do this, what do I have to do to make it work (I looked
at "eval" but gave up kinda quick on it)?

You gave up too soon. While there are other methods, eval() is the
simplest way to compile a bit of code that has been handed in as an
argument:

my $filter_code = shift;
eval "sub filter { $filter_code }";
die "Invalid filter code '$filter_code': $@" if $@;

while ( <DATA> ) {
/(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+/;
print filter() ? "yes " : "no ", $_;
}

__DATA__
a b c d
a a a a
a b a b
a a c c

Anno
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top