[Closed] Dynamic Variables and Arrays
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
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
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: %
" 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: %
" 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…
Thank you very much for your responses. I was going about things completely the wrong way.
Everything is Wai now,
Thanks again,
–J