You are trying to re-invent a wheel that is already turning very nicely:SQL::Abstract. It allows you to turn a Perl-data structure directly into an SQL-statement.
use strict; use SQL::Abstract; my @all_names = qw/one two three/; my @email = qw/first_email second_email third_email/; my @local = qw/here there and_everywhere/; my @year = qw/1970 1980 1990/; my @order = qw/location name/; my $sql = SQL::Abstract->new; my @fields = qw/name email addr location/; my $table = 'my_users'; my %where = ( year => \@year, email => \@email, location => \@local, name => \@all_names, ); my ( $stmt, @bind ) = $sql->select( $table, \@fields, \%where, \@order + ); print "$stmt\n"; print join '|', @bind;
The generated SQL and array of values looks like (I added some lay-out and delimiters for clarity):
SELECT name, email, addr, location FROM my_users WHERE ( ( ( email = ? OR email = ? OR email = ? ) AND ( location = ? OR location = ? OR location = ? ) AND ( name = ? OR name = ? OR name = ? ) AND ( year = ? OR year = ? OR year = ? ) ) ) ORDER BY location, name first_email|second_email|third_email| here|there|and_everywhere| one|two|three| 1970|1980|1990
This data can then be handed directly to your DBI handle:
my $sth = $dbh->prepare($stmt); $sth->execute(@bind);
As the author of SQL::Abstract so eloquently said : "Easy, eh?".

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


In reply to Re: DBI, MAP Help! by CountZero
in thread DBI, MAP Help! by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.