attempting to return values from array from w/in a function

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am attempting to read through a text file & then update an HTML page
table w/ the output from the text file (ie; statusing by table).

What I want the code to do is read through the file, look for a line
that matches a couple of strings, next is loop through the file from
that match to 30 lines searching for a "score" from, then store that
score; and then look for the next algorithm (8 of them) run & score.

What I get is an array w/ only the most recent array entry.

I have tried several things; return(), array_push(), php manual, and a
more than a few days searching online for a similar example.

Any help is greatly appreciated.

Code example below;

function fraction_0($file,$log) {
  while (! feof($file)) {
    $line = fgets($file,512);
      if (ereg($_GET[what],$line) && ereg("_A_0",$line) &&
ereg("Request ID",$line)) {
      print ("$log".htmlspecialchars($line)."<BR>");
      for ($i30 = 1; $i30 < 30; $i30++){
      $line = fgets($file,512);
        if (!ereg("returned best solution",$line)){
          if (ereg("Score from run", $line)){
            print ("$log".htmlspecialchars($line)."<BR>");
            $alg_score = explode(" ", $line, 512);
              if (ereg($alg_score[6], 1)) {
              $alg_array = array("alg0_1" => $alg_score[8]);
              print "score for alg 1 is $alg_array[alg0_1]<BR>";
              }elseif (ereg($alg_score[6], 2)) {
              $alg_array = array("alg0_2" => $alg_score[8]);
              } elseif (ereg($alg_score[6], 3)) {
              $alg_array = array("alg0_3" => $alg_score[8]);
              } elseif (ereg($alg_score[6], 4)) {
              $alg_array = array("alg0_4" => $alg_score[8]);
              } elseif (ereg($alg_score[6], 5)) {
              $alg_array = array("alg0_5" => $alg_score[8]);
              } elseif (ereg($alg_score[6], 6)) {
              $alg_array = array("alg0_6" => $alg_score[8]);
              } elseif (ereg($alg_score[6], 7)) {
              $alg_array = array("alg0_7" => $alg_score[8]);
              } elseif (ereg($alg_score[6], 8)) {
              $alg_array = array("alg0_8" => $alg_score[8]);
          print "<table border=\"1\"><tr><th>Alg 1</th><th>Alg 2</
th><th>Alg 3</th><th>Alg 4</th><th>Alg 5</th><th>Alg 6</th><th>Alg 7</
th><th>Alg 8</th></tr>";
          print "<tr><td>$alg_array[alg0_1]</td><td>
          print "</table>";
          } else {
            print ("$log".htmlspecialchars($line)."<BR>");

Caveat; I am new to PHP (~2weeks) & do not have formal scripting
background; sorry if this question is rudimentary.


Re: attempting to return values from array from w/in a function

At Fri, 01 Jun 2007 03:58:00 +0000, jef.d let h(is|er) monkeys type:

Quoted text here. Click to load it

No worries, good thing to mention you are new to PHP. We don't bite.

0: When debugging, set error_reporting (E_ALL) to catch all hints and
warnings PHP spits out.

1: Don't use ereg, it's deprecated. If you need regex, use preg_match or

2. Don't use regex if regular string functions suffice. Think strpos() or
strstr(). If you use strpos, make sure you explicitly check for FALSE
value using  === rather than == (which also matches a value of 0). Regex
is slow, costly in resources terms.

3. Put quotes around non-numerical array keys. $array['mylabel'], and not
$array[label]. (Though this will give the same result in many cases,
unless a constant was defined with the same name. But it will through a

4. ereg (arrayvalue, numerical value) makes no sense. By implicit casting
you end up comparing strings, but the test you mean probably is simply:
if ($array[1] == 2) etc.

5. consider replacing constructs like if ($a == 1) elseif ($a == 2) etc
with: switch ($a) {
  case 1 :
  case 2 :
  default :
// default : is optional, often you should never get here, but it can help
you debug and is formally correct}
// remember to end each case with break; to prevent 'falling through' to
the next case codeblock.(unless this is what you want)

6. You can read in an entire file (if it's not a monstruously large one)
using file(), which stores all lines in an array, or
file_get_contents(), which reads the entire file as a string.

Maybe after repairing your code along these lines (there are many other
ways and opinions on what's the optimal/desired strategy, mine is but one)
you are able to find the error in your code.

If not, repost the script and preferrably a snippet of the data you parse.  
Without that it's hard to say where and why your code fails.


Schraalhans Keukenmeester -
[Remove the lowercase part of Spamtrap to send me a message]

  "strcmp('apples','oranges') < 0"

Site Timeline