Collection within a Collection
Blitz3D Forums/Blitz3D Programming/Collection within a Collection
| ||
A collection within a collection within an array. Sounds complicated, doesn't it? I don't know whether this should be possible or not, but the following test program does compile and run... Type Maxi Field Val Field List.Mini End Type Type Mini Field SubValue End Type Dim A.maxi(5) A.Maxi(1) = New Maxi A(1)\Val=1 A(1)\List.Mini = New Mini A(1)\List\SubValue = 13 A(1)\List.Mini = New Mini A(1)\List\SubValue = 17 A.Maxi(2) = New Maxi A(2)\Val=2 A(2)\List.Mini = New Mini A(2)\List\SubValue = 14 A(2)\List.Mini = New Mini A(2)\List\SubValue = 18 Print A(1)\Val A(1)\List.Mini = First Mini Print A(1)\List\SubValue A(1)\List = After A(1)\List Print A(1)\List\SubValue Print A(2)\Val A(2)\List.Mini = First Mini Print A(2)\List\SubValue A(2)\List = After A(2)\List Print A(2)\List\SubValue Trouble is, it doesn't work the way it should. It should print these numbers: 1,13,17,2,14,18 ...but instead, it produces this list: 1,13,17,2,13,17 So question is, have I made a mistake or uncovered an obscure bug? |
| ||
Use them all the time. |
| ||
You have created two lists (Maxi,Mini). Maxi : 1,2 Mini : 13,14,17,18 This line A(x)\List.Mini = First Mini goes to the very first in the Mini list, not matter what value x is. You have not created a sub list system, just some cross links. |
| ||
You have created two lists (Maxi,Mini). No. I've created two type definitions. Each item in "Maxi" should contain a seperate list of "Mini" items. The values assigned to the Mini list in the second element of the array appear to have been confused for those assigned to the first element. ...or have I misunderstood the way this stuff is intended to work? |
| ||
or have I misunderstood the way this stuff is intended to work? Yes.You've done exactly what Ziltch said above. |
| ||
Yes, I get it now. ...Aargh. I'll have to do some serious rethinking about how I use this functionality now. Thanks for the help. :) |
| ||
forget about the array and do:Type Maxi Field Val Field FirstMini.Mini End Type Type Mini Field SubValue Field NextMini.Mini End Type |
| ||
Thanks for that-- a little more complex than I need for my purposes, but a useful tip to keep in mind. The code sample above doesn't really give the clearest indication of what I'm trying to do, or why I really can't give up on using the array structure. In fact, I'm using a two-dimensional array containing scripts of various lengths. For a moment I thought this was going to be a big problem, where I'd have to totally restructure my code, but a simple solution appeared quickly... Now that I've realised that my array doesn't actually hold any data, but only pointers to the container, I've simply made each array item always point to the start of its script data. I've also added an additional "END" script entry. When it comes time to run through the script items at a given element of the array, I'll use the array to point me to the start of the script, and stop when I come to the "END" script item. All script data gets loaded in all at once, so scripts are added in their entirety--no need to worry about linking up items in various locations in the container. |