How do I address individual elements in a 2-Dimensional array


(Robert Nyholm) #1

I have 16-element global arrays as follows:
var List ArraySun = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var List ArrayMon = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var List ArrayTue = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var List ArrayWed = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var List ArrayThu = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var List ArrayFri = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var List ArraySat = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

and one 2-dimensional array (is this the correct syntax?)

var List ArrayWeek = newArrayList(ArrayMon, ArrayTue, ArrayWed, ArrayThu, ArrayFri, ArraySat, ArraySun) // is this the correct way to create an array of arrays?

In my rules I write and read the 1-Dimensional arrays via .set and .get as follows:

MyNumber = (Sunday.state as Number).intValue
ArraySun.set(MyIndex, MyNumber)

and

MyNumber = ArraySun.get(MyIndex)
etc.

Now I would like to read individual elements from the 2-dimensional array while inside 2 loops:
MyDay = 0
MyIndex = 0
while (MyDay < 7) {
while (MyIndex < 16) {
MyNumber = ArrayWeek.get(MyDay, MyIndex) // is this correct ?
MyArray = ArrayWeek.get(MyDay) //or do I need to do something
MyNumber = MyArray.get(MyIndex) // like this
logInfo(“Array Test”, ">>>>> RUNNING RULE = Array Test. MyNumber = " + MyNumber)
MyIndex = MyIndex + 1
}
MyDay = MyDay + 1
}

Can someone help me with the correct method and syntax to CREATE the 2-Dimensional array, ie. is “var List ArrayWeek = …” correct?
And with the correct method and syntax to READ individual elements from the 2-dimensional array?
And if I need in the future, the correct method and syntax to WRITE individual elements from the 2-dimensional array?

Thanks in advance…bob


(Robert Nyholm) #2

CORRECTION - I see my copy and paste did not work correctly… The array “List” includes the type as follows:

I have 16-element global arrays as follows:
var List ArraySun = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var List ArrayMon = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var List ArrayTue = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var List ArrayWed = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var List ArrayThu = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var List ArrayFri = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
var List ArraySat = newArrayList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

and one 2-dimensional array (is this the correct syntax?)

var List ArrayWeek = newArrayList(ArrayMon, ArrayTue, ArrayWed, ArrayThu, ArrayFri, ArraySat, ArraySun) // is this the correct way to create an array of arrays?


(Robert Nyholm) #3

This window editor is giving me problems. Each of the 7 day arrays have type “Integer” captured by lazy v’s after “var List”. The 2-dimensional array has type “Array” captured by lazy v’s after “var List”.


(Lou Erickson) #4

See How To Use Code Fences to get that code formatted readably. It’ll help a lot.


(Rich Koshak) #5

Here is the thing with the Rules DSL. The correct answer is to not try to use a 2D array in Rules DSL. There are other ways you can go about this but honestly, if this is the style of programming you want to employ, you should consider using the JSR223 add-on and one of those languages (Jython is the most popular right now but JavaScript and Groovy are fully supported). You will have full access to all the sorts of language features you are used to.

If you decide to stick with Rules DSL, You will have to bend your style to the language rather than trying to make the language bend to your style.

Now to answer your specific question, 2D arrays are not supported. End of story. You could create an ArrayList of ArrayLists as a sort of poor man’s 2D array and you would use myarray.get(1).get(4) to read and myarry.set(1).set(4) to write.

But there are probably other ways to achieve your ultimate goal in the Rules DSL, should you decide to persevere. Look at the Design Pattern postings, in particular Working with Groups in Rules and Encoding Values. If you describe the over all thing you are trying to accomplish (not how you think you want to accomplish it) I can perhaps provide more useful advice.