Array Lookup Table

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Array Lookup Table

Post by Jonah Bron »

Hello, world!

A few months ago, one of the gurus (could have been PCSpectra, onion2k, not sure) here demonstrated the use of an array as a lookup table without actually have a huge array; it worked with conditions or something. Would someone kindly show that again? I tried to google it, and search the forum real quick, but didn't find anything right off.

Thanks.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Array Lookup Table

Post by McInfo »

Was it topic 112042? If not, I think there might have been another topic dealing with lookup tables shortly after that.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Array Lookup Table

Post by Jonah Bron »

Yes, that's it, thank you. The format is a little confusing though, I'll have to experiment with it. :)

Edit: Ah, I see. It just evaluates those conditions and the result is the key. So it's either 0 or 1. The last wrong one would be at index 0, and the last true one would be at index 1. That's cool.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Array Lookup Table

Post by Jonah Bron »

I see an advantage of a Switch statement over this, and that is all conditions are checked, not just enough to get by.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Array Lookup Table

Post by Weirdan »

Jonah Bron wrote:I see an advantage of a Switch statement over this, and that is all conditions are checked, not just enough to get by.
I don't think I'm following. In switch(true) {...} the first condition that evaluates to true would be executed, ignoring other conditions. In logical gate (let's call it like this) all conditions would be evaluated and the last evaluated to true would be used. To clarify, I mean something like this:

Code: Select all

function switch_switch($x, $y) {
   switch (true) {
        case $y >= 100           : return 100 + $x;
        case $y>= 50 && $y < 100 : return 50 + $x;
        case $y >= 1 && $y < 50  : return $y + $x;
        case $y < 1 : default    : return 0;
   }
}
function logicalGate_switch($x, $y) {
    $answerMap = array(
        $y < 1                  => 0,
        $y >= 1 && $y < 50      => $y + $x,
        $y >= 50 && $y < 100    => 50 + $x,
        $y >= 100               => 100 + $x,
    );
 
    return $answerMap[true];
}
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Array Lookup Table

Post by Jonah Bron »

That's exactly what I mean. Switch only checks enough to find a true one, while the lookup array method checks them all, which is obviously less efficient (although possibly negligible).
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Array Lookup Table

Post by VladSun »

It has been discussed already:
viewtopic.php?f=50&t=113253
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply