Okay, so I'm working on a PHP script to ease my creation of very large environment related arrays for Yumé:tED; however, I'm having difficulty with two things.

For one, I have a logic issue somewhere in my code as my test data output looks like this:


Code:
{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{,,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};


As you can see, it has bizarre extra commas at the beginning of the 2nd group of array values on up. My code is:

Code:
<?php


$data = file_get_contents("./arraygen/data.txt");

$dataarray = explode("|", $data);


for($i = 0; $i < count($dataarray); $i++)
{
   $tmp = explode(" ",$dataarray[$i]);

   for($j = 0; $j < count($tmp); $j++)
   {
      $tmpstr = $tmp[$j];
      for($z = 0; $z < strlen($tmpstr); $z++)
      {
            $finarray[$j] .= substr($tmpstr, $z, 1).",";
      }
   }

   $finstring = implode("", $finarray);

   echo($finstring); //Ignore this and the next two lines, they're just debug I left in.
   echo("\n");
   echo("\n");

   $finstring = substr($finstring, 0, strlen($finstring)-1);
   $finstring = "{".$finstring."}";

   $ultarray[] = $finstring;

   $finstring = NULL;
   $finarray = NULL;

}

$ultstring = implode (",", $ultarray);


$ultstring = "{".$ultstring."};";

$ultstring = str_replace(',,', "", $ultstring);

$file = fopen("./arraygen/result.txt", "w");
fwrite($file, $ultstring);
fclose($file);


?>


and my test input is

Code:
000000000000 000000000000 000000000000 000000000000 000000000000|
000000000000 000000000000 000000000000 000000000000 000000000000|
000000000000 000000000000 000000000000 000000000000 000000000000|
000000000000 000000000000 000000000000 000000000000 000000000000|
000000000000 000000000000 000000000000 000000000000 000000000000|
000000000000 000000000000 000000000000 000000000000 000000000000|
000000000000 000000000000 000000000000 000000000000 000000000000



Also, as you may or may not notice:

Code:
$ultstring = str_replace(',,', "", $ultstring);

The above line does nothing whatsoever. It's my attempt at simply fixing the output because I couldn't figure out what was causing the issue in the first place.

So, what's causing the error and why won't str_replace do its job?
Right off the top of my head, I'm thinking you have some unprintable character between those two commas. Can you please:


Code:
$finarray[$j] .= substr($tmpstr, $z, 1).",";
ABOVE, ADD
if (substr($tmpstr, $z, 1) != '0')
    echo "Odd character found: #".ord(substr($tmpstr, $z, 1));


Obviously this will only work for your contrived all-zeros example, but still.
Ahah! Your speculation was correction. I'm guessing it was a combination of \r and \n as a quick $tmpstr = trim($tmpstr); cleared the problem up right quick. Thanks, Kerm! Smile
TsukasaZX wrote:
Ahah! Your speculation was correction. I'm guessing it was a combination of \r and \n as a quick $tmpstr = trim($tmpstr); cleared the problem up right quick. Thanks, Kerm! Smile
My pleasure, glad to help! Smile Feel free to keep using this topic for any PHP issues you might have problems with. Might I guess that this is for generating maps and such for your game?
You are absolutely correct, my good Kerm Razz
TsukasaZX wrote:
You are absolutely correct, my good Kerm Razz
Excellent, I hope that it's going well. I also hope that you're considering something more efficient than a single large, flat mapspace for the sake of efficiency and scalability.
KermMartian wrote:
TsukasaZX wrote:
You are absolutely correct, my good Kerm Razz
Excellent, I hope that it's going well. I also hope that you're considering something more efficient than a single large, flat mapspace for the sake of efficiency and scalability.


I'll think about it later.
You could easily come up with something like a multidimensional tree, where your top level is a grid of pointers to arrays; the top level is your whole word map, and the next level is the actual maps divided up. If it was huge, you could even have three levels (or make the top level larger).
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement