Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > test if statement from database

Reply
Thread Tools

test if statement from database

 
 
ef
Guest
Posts: n/a
 
      05-18-2005
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.
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-18-2005
ef wrote:
> 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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      05-18-2005
ef <(E-Mail Removed)> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      05-18-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:20050518134749.474$(E-Mail Removed):

> ef <(E-Mail Removed)> wrote:


>> 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 ){
>> .....
>> }

....
> 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,$op,$constant) = $fromdb =~ /$(.*) (<|=|>) (.*)$/ or die;

> 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

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      05-18-2005
"A. Sinan Unur" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote in news:20050518134749.474$(E-Mail Removed):
>
> > ef <(E-Mail Removed)> wrote:

>
> >> 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 ){
> >> .....
> >> }

> ...
> > 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,$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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
ef
Guest
Posts: n/a
 
      05-18-2005
Thanks for the suggestions, i will test it.

Bo.

(E-Mail Removed) wrote in news:20050518140902.496$(E-Mail Removed):

> "A. Sinan Unur" <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote in news:20050518134749.474$(E-Mail Removed):
>>
>> > ef <(E-Mail Removed)> wrote:

>>
>> >> 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 ){
>> >> .....
>> >> }

>> ...
>> > 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,$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
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
TEST TEST Test...Blah Blah Blah Generalbatguano@pacbell.net Computer Support 6 09-13-2006 01:53 AM
TEST TEST TEST Gazwad Computer Support 2 09-05-2003 07:32 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments