test if statement from database

Discussion in 'Perl Misc' started by ef, May 18, 2005.

  1. ef

    ef Guest

    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.
     
    ef, May 18, 2005
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, May 18, 2005
    #2
    1. Advertising

  3. ef

    Guest

    ef <> 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
     
    , May 18, 2005
    #3
  4. wrote in news:20050518134749.474$:

    > ef <> 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, May 18, 2005
    #4
  5. ef

    Guest

    "A. Sinan Unur" <> wrote:
    > wrote in news:20050518134749.474$:
    >
    > > ef <> 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
     
    , May 18, 2005
    #5
  6. ef

    ef Guest

    Thanks for the suggestions, i will test it.

    Bo.

    wrote in news:20050518140902.496$:

    > "A. Sinan Unur" <> wrote:
    >> wrote in news:20050518134749.474$:
    >>
    >> > ef <> 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
    >
     
    ef, May 18, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    835
  2. Jay McGavren
    Replies:
    11
    Views:
    1,185
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,164
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    495
    Duncan Booth
    Jul 22, 2004
  5. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    765
    Skybuck Flying
    Oct 4, 2009
Loading...

Share This Page