test if statement from database

E

ef

Hi,

I have an compare statement stored in the database. e.g. 'fieldx > 10'.
I read that and want to use that directly in a perl statement, something
like

$iffromdatabase =~ s/fieldx/realvalueoffieldx/;
if ( /$iffromdatabase/e ){
.....
}

can this be done somehow?

Bo.
 
G

Gunnar Hjalmarsson

ef said:
I have an compare statement stored in the database. e.g. 'fieldx > 10'.
I read that and want

Did you read *why* you want it?
to use that directly in a perl statement, something like

$iffromdatabase =~ s/fieldx/realvalueoffieldx/;
if ( /$iffromdatabase/e ){
.....
}

can this be done somehow?

Yes. Check e.g.

perldoc -q "expand variables"
perldoc -f eval
 
X

xhoster

ef said:
Hi,

I have an compare statement stored in the database. e.g. 'fieldx > 10'.
I read that and want to use that directly in a perl statement, something
like

$iffromdatabase =~ s/fieldx/realvalueoffieldx/;
if ( /$iffromdatabase/e ){
.....
}

can this be done somehow?

You could probably do this with eval, but you would probably be better
off doing something like:

my ($field,$op,$constant) = $fromdb =~ /$(.*) (>) (.*)$/ or die;

if (apply_logic($valueof{$field},$op,$constant)) {
#...
};

sub apply_logic {
return $_[0] < $_[2] if $_[1] eq '<';
return $_[0] > $_[2] if $_[1] eq '>';
return $_[0] == $_[2] if $_[1] eq '==';
die "Don't know how to $_[1]"
};

Xho
 
A

A. Sinan Unur

....
You could probably do this with eval, but you would probably be better
off doing something like:

my ($field,$op,$constant) = $fromdb =~ /$(.*) (>) (.*)$/ or die;

Based on apply_logic below, this should probably be (untested):

my ($field said:
sub apply_logic {
return $_[0] < $_[2] if $_[1] eq '<';
return $_[0] > $_[2] if $_[1] eq '>';
return $_[0] == $_[2] if $_[1] eq '==';
die "Don't know how to $_[1]"
};

Nice, very nice.

Sinan
 
X

xhoster

A. Sinan Unur said:
Based on apply_logic below, this should probably be (untested):

my ($field,$op,$constant) = $fromdb =~ /$(.*) (<|=|>) (.*)$/ or die;

Yes, and also the first $ in the regex should be ^.
(But still untested).


Come to think of it, just
my ($field,$op,$constant)= split ' ', $fromdb;
might be better, as then the operator only needs be validated in
apply_logic rather than in two places. Provided of course that
white space is appropriate as a delimiter.


Xho
 
E

ef

Thanks for the suggestions, i will test it.

Bo.

Yes, and also the first $ in the regex should be ^.
(But still untested).


Come to think of it, just
my ($field,$op,$constant)= split ' ', $fromdb;
might be better, as then the operator only needs be validated in
apply_logic rather than in two places. Provided of course that
white space is appropriate as a delimiter.


Xho
 

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,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top