PDA

View Full Version : Dynamic Variables and Arrays


Jonbojangles
03-23-2006, 03:33 AM
I would like to check dynamically check each array for the value "1" and return the arrays count based off of that. problem is I do not know how to dynamically change a variable. I will have many arrays so I would prefer to have this done dynamic if possible.


array1 = #(1,2,3,4,5,6,7,8)
array2 = #()
array3 = #(1,2)

c=1
for c = 0 to 2 do(
dd=c+1
arr = ("array" + (dd as string)) --RETURNS STRING
--arr = arr + dd **DOES NOT WORK**
print arr --prints the string (array1, array2, array3)

if (matchPattern (arr as string) pattern: "1") then(
print (arr.count) --Print count of array
print (arr as string) --print out array string
)
)


Thank you for any help on this.

--Jon

stuh505
03-23-2006, 04:02 AM
I don't know what you're trying to do, but..

arr = ("array" + (dd as string)) --RETURNS STRING
--arr = arr + dd **DOES NOT WORK**

Ok, so arr is a string...and in the next line you attempt to add it to dd which is an integer. That's why it doesnt work

JohnSwafford
03-23-2006, 08:27 PM
It seems like you're needlessly converting your integer values to strings and then searching the strings for "1". I might be missing what you want, but it seems like you should just loop through the arrays and directly test for the integer value of 1.

Example:

(
local array1 = #(1,2,3,4,5,6,7,8)

for i=1 to array1.count where array1[i]==1 do
(
format "Number 1 found in array1 at index: %\n" i
)
)


If you want to search multiple arrays, its easier if you nest the arrays in a larger array...

Example:

(
local array1 = #(1,2,3,4,5,6,7,8)
local array2 = #()
local array3 = #(1,2)
local BigArray = #(array1, array2, array3)

for bai=1 to BigArray.count do
(
for i=1 to BigArray[bai].count where BigArray[bai][i]==1 do
(
format "Number 1 found in subarray % at index: %\n" bai i
)
)
)


I've been working on a project for 2 years that involves nested arrays like this that are 8-9 levels deep. Its more complex when you're learning, but very useful/powerful once you get used to working that way....

Jonbojangles
03-27-2006, 08:07 AM
Thank you very much for your responses. I was going about things completely the wrong way.
Everything is Wai now,

Thanks again,

--J

CGTalk Moderation
03-27-2006, 08:07 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.