Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
If your query consists of embedding the available data into a string in a specific order, you could just interpolate it:
my $query = ":$name:$vorname:$plz:$tel:$tel49:";
If the query structure needs to look different for each combination of data, you could use the bit vector you're constructing to access a hash:
my %query_for = ( 0 => "::", 1 => "name IS \$name", 2 => "vorname IS \$name", ... );
You'd need 2^5 (32) entries in the hash if every possible combination of the five items was valid; if not, you could leave out any invalid combinations. This means that if $query_for{$bit_vector} was undef, you'd know you had an invalid combination without any comparison logic at all.

Note that I escaped the "query" strings; that way, you can do an eval on the string and interpolate the variables into the query at the time you have the values you want. If the variables weren't escaped, whatever values they had (probably undef) at the time the hash was constructed would be substituted in immediately, and you'd always get queries without any data in them.

A much more advanced way to do it is to use anonymous subroutines as the entries in the hash:

my %query_constuctor_for = ( ... 18 => sub { return ":NIL/$vorname::$tel49:" }, 19 => sub { return ":$name/$vorname::$tel49:" }, ... );
Lookup works the same way, but you'd say
my $query_maker = $query_constructor_for{$bitvector}; defined $query_maker or die "Combination invalid"; $query = $query_maker->()
to get your query; the $query_maker->() expression calls the anonymous subroutine that you looked up with $query_constructor_for{$bitvector}. This lets you construct arbitrarily-complicated code to do what's needed for each combination.

In reply to Re^4: A Case with 5 Var's by pemungkah
in thread A Case with 5 Var's by ultibuzz

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (5)
    As of 2014-04-19 13:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (480 votes), past polls