Page 1 of 1

Developers challenge

Posted: Thu Apr 27, 2006 4:50 pm
by alex.barylski
Ok, so ever suggests the method I use...wins a million dollars and the respect and admiration of the community for one day :P

Just kidding of course...but it's an interesting problem...and I'll be sure to add your name and credits to the function comments

I have a function, which is basically scanning one array and comparing it's values to another array and conducting a trivial multi-field comparison...

Like a SQL:

Code: Select all

SELECT * FROM table WHERE name = "Bob" and age = 97
Only I'm using native PHP and arrays...

The way I match criteria and conclude an answer is by comparing each item against another and the result of that basic test is stored or pushed onto an array...basic boolean test will always yield TRUE or FALSE(1 or 0)...

At the end of looping I then compare the number of elements in the array which holds the indivual results to the SUM of all values in the array which holds results...

Code: Select all

$arr[0] = 1; // Username test
$arr[1] = 1; // Postal test
$arr[2] = 1; // Address test
...
By seeing the above it becomes obvious that a quick way to determine if each result was TRUE (ultimately returning TRUE as each criterion was met) is to SUM the values and compare that to the count() of the array...

This is how I've done it thus far...but I am convinced there is a better way...by I am brain dead for the day...at least when working on this... :)

So what do you think?

Can you come up with a more effective & efficient solution?

I've implemented a second solution, but I have to benchmark it first...

I'm curious what someone else can come up with...

Cheers :)

Posted: Thu Apr 27, 2006 4:51 pm
by alex.barylski
I should note...

I'm looking for fairly specific solutions heres, not quick and dirty suggestions...

I wanna see what you can actually come up with...not theorize... ;)

Cheers :)

Posted: Thu Apr 27, 2006 5:00 pm
by feyd
If I'm only looking for pass/fail over the whole thing I always use a single boolean.

Code: Select all

$flag = true; // initialize

$flag &= true; // some check, it passed. The flag stays in passed state.
$flag &= false; // another check, it failed. The flag now stays failed for the remainder of all tests.
Old C tricks die hard. :)

Posted: Thu Apr 27, 2006 5:07 pm
by alex.barylski
feyd wrote:If I'm only looking for pass/fail over the whole thing I always use a single boolean.

Code: Select all

$flag = true; // initialize

$flag &= true; // some check, it passed. The flag stays in passed state.
$flag &= false; // another check, it failed. The flag now stays failed for the remainder of all tests.
Old C tricks die hard. :)
Nice... :D

My second solution used some fancy bitwise operations too, but for whatever reason I wanted to keep track of which tests failed and those that didn't...

When I first read your post I was going to respond with that won't work cuz...you loose which test(s) failed...

However it made me re-think my solution and decide that wasn't *really* nessecary...as it's only handy in debug mode...so I dropped it in favour of a single state variable...

Thanks for that :)

Cheers :)

Posted: Fri Apr 28, 2006 1:27 pm
by n00b Saibot
Hockey wrote:My second solution used some fancy bitwise operations too, but for whatever reason I wanted to keep track of which tests failed and those that didn't...
I thought of bitwise storage the instant I read your first post. but when you said this, I came to conclude that you didn't use bitwise the right way. I would use it like this

Code: Select all

<?php
$tests = array(1, 0, 1, 1, 1, 1); //let's suppose this is the test result array...
$result = 0; //this will hold compact results & final outcome combined. Now, ain't that neat... ;)

for($i = 0; $i < count ($tests); $i++)
 $result |= $tests[$i] << $i;

print $result;
?>
this way you can keep track of which tests returned what and still get a clear picture whether all failed or som passed... 8)

These techniques I bring from my C experience. I used bitwise structures a lot since they took up so less space and were easy to store and pass around in memory. Like feyd said, Old C tricks die hard.