PDA

View Full Version : Arrays within arrays


jHromika
02-08-2004, 11:05 PM
In order to clean up a bit of code I'm working on, I'd like to store a couple string arrays in one sort of master array so I could just loop the whole process.

Here's some sample code because I think it might be easier to read out of context (not to mention, you could actually run it)

And my apologies for reading it without tabs, doesn't seem to like that or spaces from the preview window.


// It doesn't like me defining arrays without scaler values within the masterArray, hence using $j. I'm assuming that's why I'm getting only the first seeds from each array, but this was the only way I could run the script so far...

int $j = 0;

string $array1[] = {"This ", "should ", "work."};
string $array2[] = {"I ", "think."};
string $array3[] = {"Why ", "won't ", "it?"};

string $masterArray[] = {$array1[$j], $array2[$j], $array3[$j]};

string $currentWord;
string $currentArray[];

for ($i = 0; $i <= 2; $i++)
{
$currentArray = {$masterArray[$i]};

for ($currentWord in $currentArray)
{
print $currentWord;
$j += 1;
}

print "\n";

}


So basically, what I want from that, is to cycle the whole thing 3 times. Retrive array[$i] from the masterArray, and then for each slot in array[$i] I want it to be printed out.

What I'm getting instead is slot 0 of each array.

So instead of...

This should work.
I think.
Why doesn't it?

I get...

This
I
Why

So far I haven't see any documentation on this and my guess and check methods aren't coming up with much.

Anyone have any thoughts?

jHromika
02-08-2004, 11:31 PM
Well, I got up away from the computer, took a shower, thought about my problem a bit, and came up with an alternate solution.


int $k = 0;

string $masterArray[] = {"This ", "should ", "work.", "I ", "think.", "Why ", "won't ", "it?"};

int $countArray[] = {2, 1, 2};

for ($i = 0; $i <= 2; $i++)
{

for ($j = 0; $j <= $countArray[$i]; $j++)
{
print $masterArray[$k];
$k += 1;
}

print "\n";
}



I put all of my array values into one main array, and made a second array with the would be size values of the multiple arrays. This way I could cycle though the main array as if they were separated like I originally planned. I added int $k to keep track of my position through the master array.

Ironically, this method is even shorter...

I'd still be curious if anyone could help me with storing arrays within arrays though. That seems like it could be extremely useful in certain situations.

bigfatMELon
02-09-2004, 07:51 AM
Check out my ArrayTools package on Highend3D. Its lets you create and manage the equivalent of string matrices or compound string arrays within MEL by encoding/decoding arrays to and from plain string format.

-jl

CGTalk Moderation
01-17-2006, 10:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.