472,127 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

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

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

Jun 1 '07 #1
1 1595
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 - sc*********@the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

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

Jun 1 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Andrew Poulos | last post: by
4 posts views Thread by Isaac | last post: by
4 posts views Thread by Woody Splawn | last post: by
6 posts views Thread by alternativa | last post: by
2 posts views Thread by mosesdinakaran | last post: by
18 posts views Thread by Pedro Pinto | last post: by
19 posts views Thread by Andrew Gentile | last post: by
reply views Thread by =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.