Syntactic Confectionery Delight PerlMonks

### Re^4: A Case with 5 Var's

by eric256 (Parson)
 on Jan 25, 2007 at 15:38 UTC ( #596519=note: print w/replies, xml ) Need Help??

in reply to Re^3: A Case with 5 Var's
in thread A Case with 5 Var's

It depends how your query is built. Hopefully each variable is responsible for just one section, then you just build the query up as you go.

``` my \$qry  = "";
my \$name = "Mueller";
my \$plz  = "55555";
if (\$name) { \$qry .= "::5544\$nameGDFT"; }
if (\$plz)  { \$qry .= "xxxxx\$plz::0:";}
print \$qry;

Since I have no idea what kind of query that is I had no idea what parts go where, but hopefully that gives you and idea how it can be done.

If your query is completly dependent on the elements available then you could combine your ifs together some for readability at least

``` if    (\$name && \$vorname && \$plz && \$tel && tel49) { \$qry = "whatever
+" }
elsif (\$name && \$vorname && \$plz && \$tel)          { \$qry = "whatever
+2" }
elsif (\$name && \$vorname && \$plz)                  { \$qry = "whatever
+3" };

That is not the recommended solution, just one of the many ways to do it.

___________
Eric Hodges

Replies are listed 'Best First'.
Re^5: A Case with 5 Var's
by ultibuzz (Monk) on Jan 25, 2007 at 16:11 UTC

yep it depends on the elements, and with && i need much more if and elsifs.
in my example i don't need to check \$name later on when it was not there so its much shorter, all var's combination with && are a bit more ;)
kd ultibuzz

Actually I would think it comes out to the same number of if's but in a more readable format where you can see immediately which elements had to be true to force that query, either way I would recommend building the query on the fly if that is at all possible. Either way this is 25 lines instead of 70ish.

```if (\$1 && \$2 && \$3) { #a }
if (\$1 && \$2)       { #b }
if (\$1 && \$3)       { #c }

versus

```if (\$1) {
if (\$2) {
if (\$3) {
#a
} else {
#b
}
} elsif (\$3) {
#c
}
}

I got a little bored and tried it out. Unless i screwed something up I think you are messing several combinations.

```if    (\$name    && \$vorname && \$plz && \$tel && \$tel49) { \$qry = ""}

elsif (\$name    && \$vorname && \$plz && \$tel)           { \$qry = ""}
elsif (\$vorname && \$plz     && \$tel && \$tel49)         { \$qry = ""}
elsif (\$name    && \$plz     && \$tel && \$tel49)         { \$qry = ""}

elsif (\$name    && \$vorname && \$plz)                    { \$qry = ""}
elsif (\$name    && \$plz     && \$tel)                   { \$qry = ""}
elsif (\$name    && \$tel     && \$tel49)                 { \$qry = ""}
elsif (\$vorname && \$plz     && \$tel)                   { \$qry = ""}
elsif (\$plz     && \$tel     && \$tel49)                 { \$qry = ""}

elsif (\$name    && \$vorname)                           { \$qry = ""}
elsif (\$name    && \$plz)                               { \$qry = ""}
elsif (\$name    && \$tel)                               { \$qry = ""}
elsif (\$name    && \$tel49)                             { \$qry = ""}
elsif (\$vorname && \$plz)                               { \$qry = ""}
elsif (\$plz     && \$tel)                               { \$qry = ""}
elsif (\$tel     && \$tel49)                             { \$qry = ""}

elsif (\$plz)                                           { \$qry = ""}
elsif (\$name)                                          { \$qry = ""}
elsif (\$tel)                                           { \$qry = ""}
elsif (\$tel49)                                         { \$qry = ""}
elsif (\$vorname)                                       { \$qry = ""}

___________
Eric Hodges
and what is with the combination \$vorname && tel && tel49 ;)
i really like this idear of hash and a hash with all bitmasks
kd ultibuzz

Create A New User
Node Status?
node history
Node Type: note [id://596519]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2017-11-21 19:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (309 votes). Check out past polls.

Notices?