PDA

View Full Version : Inaccessible elements of an array



Xarxus
July 27th, 2023, 10:28
Ok, I admit my limits. I'm struggling with something I do no understand why is not functioning.
I've a structure (in a module) like this


<reference>
<mydata>
<id-00001>
<myfield1 type="string">My value 1</myfield1>
<myfield2 type="string">My value 2</myfield2>
<groups>
<id-00001>
<open type="string">reference.groupdata.id-00001@MyModule2</open>
<openclass type="string">group_class</openclass>
</id-00001>
</groups>
<myfield3 type="string">My value 3</myfield3>
</id-00001>
<id-00002>
<myfield1 type="string">My value 4</myfield1>
<myfield2 type="string">My value 5</myfield2>
<groups>
<id-00001>
<open type="string">reference.groupdata.id-00037@MyModule2</open>
<openclass type="string">group_class</openclass>
</id-00001>
</groups>
<myfield3 type="string">My value 6</myfield3>
</id-00002>
</mydata>
</reference>
It can happen, even often, that groups has more than one element. If I use a windowlist
on groups everything goes well. Now I need to cicle on groups, so I used this code.



function getGroup(oDataNode)
Debug.console("oDataNode", oDataNode)
if not oDataNode then
return;
end
local aGroupList = DB.getChild(oDataNode, "groups")
Debug.console("A--->", aGroupList);

for _, oElement in ipairs(DB.getChildren(aGroupList)) do
Debug.console("B-->", oElement);
end

Debug.console("C--->", DB.getChildren(aGroupList));
end
And the output is...



...
[7/27/2023 10:47:22 AM] s'oDataNode' | databasenode = { reference.mydata.id-00001@MyModule1 }
[7/27/2023 10:47:22 AM] s'A--->' | databasenode = { reference.mydata.id-00001.groups@MyModule1 }
[7/27/2023 10:47:22 AM] s'C--->' | { s'id-00001' = databasenode = { reference.mydata.id-00001.groups.id-00001@MyModule1 } }
...
[7/27/2023 10:47:22 AM] s'oDataNode' | databasenode = { reference.mydata.id-00002@MyModule1 }
[7/27/2023 10:47:22 AM] s'A--->' | databasenode = { reference.mydata.id-00002.groups@MyModule1 }
[7/27/2023 10:47:22 AM] s'C--->' | { s'id-00001' = databasenode = { reference.mydata.id-00002.groups.id-00037@MyModule1 } }
...

It doesn't enter into the for, no "B--->" is shown and I can't guess why. Consider that I'm
elsewhere in the code and the windowlist is not available (the window may not be open, and in any
case I have to operate on all the top-level elements - the calling code has its own loop that iterates
through the elements of reference.mydata and it works).

Can you help me?

Trenloe
July 27th, 2023, 10:44
You're using ipairs on a LUA table that isn't integer indexed. Use pairs.

Xarxus
July 27th, 2023, 11:00
Great! I knew I was screwing up, I was sure of it, but I was stuck and couldn't figure out where.

Ty guy!