Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: A Case with 5 Var's

by GrandFather (Cardinal)
on Jan 25, 2007 at 11:10 UTC ( #596449=note: print w/ replies, xml ) Need Help??


in reply to A Case with 5 Var's

A neat trick that may, or may not, help (depending on what you are trying to achieve) is to generate a bit vector:

use strict; use warnings; my $name = ''; my $vorname = 'full'; my $plz = 1; my $tel = 0; my $tel49 = undef; my $vector = (!$name) || 2; $vector |= (!$vorname) || 4; $vector |= (!$plz) || 8; $vector |= (!$tel) || 16; $vector |= (!$tel49) || 32; printf "%06b\n", $vector;

which sets a bit in $vector for each "true" variable. As a bonus it sets the least significant bit if any of the variables is false. Note BTW that there is a big difference between defined and "true". The sample prints:

001101

DWIM is Perl's answer to Gödel


Comment on Re: A Case with 5 Var's
Select or Download Code
Re^2: A Case with 5 Var's
by ultibuzz (Monk) on Jan 25, 2007 at 12:45 UTC

    sounds interessting, so i need to check the vector bit pattern to generate a matching qry
    and this i woud do again with if and co ;/ because i don't know another way
    kd ultibuzz

      It depends a lot on where you want to go with this. One technique would be to use a hash to generate a dispatch table. Consider:

      use strict; use warnings; my %dispatch = ( 0b000001 => \&noneTrue, 0b111110 => \&allTrue, 0b001101 => \&firstLine, ); my $name = ''; my $vorname = 'full'; my $plz = 1; my $tel = 0; my $tel49 = undef; while (<DATA>) { chomp; my ($name, $vorname, $plz, $tel, $tel49) = split ','; my $vector = (!$name) || 2; $vector |= (!$vorname) || 4; $vector |= (!$plz) || 8; $vector |= (!$tel) || 16; $vector |= (!$tel49) || 32; $dispatch{$vector}->($.) if exists $dispatch{$vector}; } sub noneTrue { print "None true in input line $_[0]\n"; } sub allTrue { print "All true in input line $_[0]\n"; } sub firstLine { print "Matched first line pattern at input line $_[0]\n"; } __DATA__ ,full,1,0, first,second,3,4, 1,2,3,4,5 ,,3,4, last,,,,

      Prints:

      Matched first line pattern at input line 1 All true in input line 3 None true in input line 5

      DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://596449]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-08-21 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (144 votes), past polls