jettero has asked for the wisdom of the Perl Monks concerning the following question:

Heyya, I just wrote a function that turns arrays into SQL where clauses thusly:
@a = (1, 2, 3, 4); @b = (6, 7, 10, 30, @a); $a = &where_funky(@a); $b = &where_funcky(@b);

$a is "where (id < 1 or id > 4)"

$b is "where (id < 6 or id > 7) and (id != 10) and (id != 30) and (id < 1 or id > 4)"

I don't even wanna show what I came up with. It's fugly. Is there a known easy way to do this?

Replies are listed 'Best First'.
Re: where clause
by t0mas (Priest) on Sep 13, 2000 at 16:38 UTC
    I would use NOT IN instead of the single value operators, like in:
    @a = (1, 2, 3, 4); @b = (6, 7, 10, 30, @a); $a = sprintf "WHERE id NOT IN (%s)" , join(",",@a); $b = sprintf "WHERE id NOT IN (%s)" , join(",",@b); print $a ."\n"; print $b ."\n";
    But that assumes that you don't have any 1.4 kinds of values...

    /brother t0mas
Re: where clause
by kilinrax (Deacon) on Sep 13, 2000 at 16:38 UTC
    Have a look at Text-Query-SQL, it might be what you need.
    Hope this helps ;-)