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:

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>
$alg_array[alg0_2]</td><td>$alg_array[alg0_3]</td><td>
$alg_array[alg0_4]</td><td>$alg_array[alg0_5]</td><td>
$alg_array[alg0_6]</td><td>$alg_array[alg0_7]</td><td>
$alg_array[alg0_8]</td></tr>";
print "</table>";
}
} else {
print ("$log".htmlspecialchars($line)."<BR>");
$i30=30;
}
}
}
}


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

Regards,
Jef

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
preg_match_all.

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
warning)

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 : {codeblock}
case 2 : {codeblock}
...
default : {codeblock}
}
// 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.

HTH
Sh.

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

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

.



Relevant Pages

  • How does PHP store strings?
    ... I've read in numerous places that PHP stores strings as an array of ... higher concept of an array so it ... seems to me that PHP strings do not require the null character at the ...
    (php.general)
  • Re: Help with php array loading/retrieval ???
    ... The problem I am having is referencing the array elements as strings ... but it appears in reality its being references as a vector/index. ... As much as I've used PHP over the years I usually ...
    (comp.lang.php)
  • Re: Query string before parsing
    ... which PHP does not handle. ... PHP has a precedent for introducing breaking changes to the language. ... This convention is a little hairy for forms that may have zero, one, or many checkboxes checked, as the handler needs to check whether the form value is a string or an array of strings, but it'd work and PHP has tools for doing that. ...
    (comp.lang.php)
  • php extension: returning array of arrays
    ... I'm trying to return an array of arrays of strings from a home-made PHP ...
    (comp.lang.php)
  • Re: Need help on PHP for MPE/ix
    ... Here is the syntax of the dbupdate intrinsic in PHP: ... assoc array of item values) ... Please let me know if you have any more suggesstions to solve this problem. ... Pavan Kumar Rati wrote: ...
    (comp.sys.hp.mpe)