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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jef.d

    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($fil e,$log) {
    while (! feof($file)) {
    $line = fgets($file,512 );
    if (ereg($_GET[what],$line) && ereg("_A_0",$li ne) &&
    ereg("Request ID",$line)) {
    print ("$log".htmlspe cialchars($line )."<BR>");
    for ($i30 = 1; $i30 < 30; $i30++){
    $line = fgets($file,512 );
    if (!ereg("returne d best solution",$line )){
    if (ereg("Score from run", $line)){
    print ("$log".htmlspe cialchars($line )."<BR>");
    $alg_score = explode(" ", $line, 512);
    if (ereg($alg_scor e[6], 1)) {
    $alg_array = array("alg0_1" =$alg_score[8]);
    print "score for alg 1 is $alg_array[alg0_1]<BR>";
    }elseif (ereg($alg_scor e[6], 2)) {
    $alg_array = array("alg0_2" =$alg_score[8]);
    } elseif (ereg($alg_scor e[6], 3)) {
    $alg_array = array("alg0_3" =$alg_score[8]);
    } elseif (ereg($alg_scor e[6], 4)) {
    $alg_array = array("alg0_4" =$alg_score[8]);
    } elseif (ereg($alg_scor e[6], 5)) {
    $alg_array = array("alg0_5" =$alg_score[8]);
    } elseif (ereg($alg_scor e[6], 6)) {
    $alg_array = array("alg0_6" =$alg_score[8]);
    } elseif (ereg($alg_scor e[6], 7)) {
    $alg_array = array("alg0_7" =$alg_score[8]);
    } elseif (ereg($alg_scor e[6], 8)) {
    $alg_array = array("alg0_8" =$alg_score[8]);
    }
    print "<table border=\"1\"><t r><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_a rray[alg0_1]</td><td>
    $alg_array[alg0_2]</td><td>$alg_arr ay[alg0_3]</td><td>
    $alg_array[alg0_4]</td><td>$alg_arr ay[alg0_5]</td><td>
    $alg_array[alg0_6]</td><td>$alg_arr ay[alg0_7]</td><td>
    $alg_array[alg0_8]</td></tr>";
    print "</table>";
    }
    } else {
    print ("$log".htmlspe cialchars($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

  • Schraalhans Keukenmeester

    #2
    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($fil e,$log) {
    while (! feof($file)) {
    $line = fgets($file,512 );
    if (ereg($_GET[what],$line) && ereg("_A_0",$li ne) &&
    ereg("Request ID",$line)) {
    print ("$log".htmlspe cialchars($line )."<BR>");
    for ($i30 = 1; $i30 < 30; $i30++){
    $line = fgets($file,512 );
    if (!ereg("returne d best solution",$line )){
    if (ereg("Score from run", $line)){
    print ("$log".htmlspe cialchars($line )."<BR>");
    $alg_score = explode(" ", $line, 512);
    if (ereg($alg_scor e[6], 1)) {
    $alg_array = array("alg0_1" =$alg_score[8]);
    print "score for alg 1 is $alg_array[alg0_1]<BR>";
    }elseif (ereg($alg_scor e[6], 2)) {
    $alg_array = array("alg0_2" =$alg_score[8]);
    } elseif (ereg($alg_scor e[6], 3)) {
    $alg_array = array("alg0_3" =$alg_score[8]);
    } elseif (ereg($alg_scor e[6], 4)) {
    $alg_array = array("alg0_4" =$alg_score[8]);
    } elseif (ereg($alg_scor e[6], 5)) {
    $alg_array = array("alg0_5" =$alg_score[8]);
    } elseif (ereg($alg_scor e[6], 6)) {
    $alg_array = array("alg0_6" =$alg_score[8]);
    } elseif (ereg($alg_scor e[6], 7)) {
    $alg_array = array("alg0_7" =$alg_score[8]);
    } elseif (ereg($alg_scor e[6], 8)) {
    $alg_array = array("alg0_8" =$alg_score[8]);
    }
    print "<table border=\"1\"><t r><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_a rray[alg0_1]</td><td>
    $alg_array[alg0_2]</td><td>$alg_arr ay[alg0_3]</td><td>
    $alg_array[alg0_4]</td><td>$alg_arr ay[alg0_5]</td><td>
    $alg_array[alg0_6]</td><td>$alg_arr ay[alg0_7]</td><td>
    $alg_array[alg0_8]</td></tr>";
    print "</table>";
    }
    } else {
    print ("$log".htmlspe cialchars($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.(unle ss 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_conten ts(), 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@the .Spamtrapexampl e.nl
    [Remove the lowercase part of Spamtrap to send me a message]

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

    Comment

    Working...