Re: [PHP] array_search function bugged? update

2006-03-27 Thread Robin Vickery
On 27/03/06, je killen <[EMAIL PROTECTED]> wrote:
> Hi all
> Here is an update on my problem reported with the array_search function.
> to those who directed me to the bug report page, thanks.
> There are a number of reports specific to this function. Maybe event
> someone else has written a fix.
> here is my solution to my problem.

There are no open bug reports for array_search(). Loads of bogus ones though.

array_search() also completely the wrong tool for the job - the code
you posted is much more complicated and slower than it needs to be.

That whole palaver that you go through to produce $reduced could be
replaced with:

   $reduced = array_merge(array(), array_unique($str));

That nested loop you use to produce $final would be rather simpler and
faster if you just flipped $reduced and used a hash lookup.

   $charmap = array_flip($reduced);
   foreach ($str as $char) { $final[] = $charmap[$char]; }

-robin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] array_search function bugged update errata

2006-03-26 Thread je killen

sorry all for the mistake in the code;
in this line 'array $input' should be string $input.
function word_wise(array $input, int $what) //returns an array
so it reads
function word_wise(string $input, int $what) //returns an array
this should be the only mistake. I added this stuff as an afterthought 
after

all the code was working perfectly in my test code.
best
JK

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] array_search function bugged? update

2006-03-26 Thread je killen

Hi all
Here is an update on my problem reported with the array_search function.
to those who directed me to the bug report page, thanks.
There are a number of reports specific to this function. Maybe event
someone else has written a fix.
here is my solution to my problem.

The following code* was developed and finalized by J.E.Killen 3/2006
* accept of course for array_search() itself.
function word_wise(array $input, int $what) //returns an array
  {$str = array();
   $stra = array();
   $str = str_split($input); //turns string into 
array
   $stra = $str; // saves a copy for final 
reconcilliation

   $tmp = '';// used to hold and transfer values
   $final = array(); // ***THE WHOLE POINT
   $reduced = array(); // reduction array
   $formula = array(); // interum array
   $output = array();  // test output.
   for($i = 0; $i < count($str); $i++)
{$formula[$i] = array_search($str[$i], 
$str);

 if($formula[$i] < $i) // use of $formula
 {$str[$i] = '';} // repeats eliminated from $str
}; // if $i is greater than formula[$i], formula[$i] is 
a repeat.
for($i = 0; $i < count($str); $i++)
   {if($str[$i] != '') // looks for empty 
strings in $str

  {
array_push($reduced, $str[$i]); // 
fills $reduced with
// non 
empty $str items
// to 
produce an array of
// all 
unique letters

   }
   };
   for($i = 0; $i < count($stra); $i++)
  {for($j = 0; $j < count($reduced); $j++)
  {
if($stra[$i] == $reduced[$j])//produces 
the final formula for
   {$final[$i] = $j; }   // 
reconstruction of the word.

  }
   }
   for($i = 0; $i < count($final); $i++)
  { $tmp = $final[$i];
$output[$i] = $reduced[$tmp]; //test run 
reconstructs word from $final and  $reduced.

   }
   switch($what) //options to chose what to return
  {case 1:
   return $reduced; //reduction to all unique 
letters

   break;
   case 2:
   return $stra;//original
   break;
   case 3:
   return $final;   // reconstruction formula
   break;
   case 4:
   return $output;  //test
   break;
}
 } // end word_wise()

Just want to return something to the php community
Jeff K

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php