PDA

View Full Version : multi-element arrays


golfandhockey
02-19-07, 11:21 PM
Hi All,

I'm not sure exactly the term, multi-element arrays, or probably structures. Anyway, need to build a multi-dimensional array. When I need the value, I got the single array and assignment:

<MvASSIGN NAME="g.fruit" INDEX="{l.index}" VALUE="{l.myfruitvalue}">

fruit[index]

Do I access the multi- by doing this:

fruit[index]:orange
fruit[index]:apple

so how do I assign a value to the element?

best regards,

GH

mvmarkus
02-20-07, 03:57 PM
Hi All,

I'm not sure exactly the term, multi-element arrays, or probably structures. Anyway, need to build a multi-dimensional array. When I need the value, I got the single array and assignment:

<MvASSIGN NAME="g.fruit" INDEX="{l.index}" VALUE="{l.myfruitvalue}">

fruit[index]

Do I access the multi- by doing this:

fruit[index]:orange
fruit[index]:apple

so how do I assign a value to the element?

best regards,

GH


Hi GH,

those are called "Arrays of structures".

You assign values like this:

Shortform:

<MvASSIGN NAME = "l.fruit" INDEX="{l.index}" MEMBER="{l.member}" VALUE = "{ l.value }" >

Longer, and more flexible:

<MvASSIGNARRAY NAME="l.fruit" VALUE="{ l.value}">
<MvDIMENSION INDEX="{l.index}">
<MvMEMBER NAME="{l.member}">
</MvASSIGNARRAY>

In the longer form, you can change the order (assigning arrays to having multi-dimensional structures) or add additional dimensions, for example:

<MvASSIGNARRAY NAME="l.fruit" VALUE="{ l.value}">
<MvMEMBER NAME="{l.member}">
<MvDIMENSION INDEX="{l.index}">
<MvMEMBER NAME="{l.member}">
<MvDIMENSION INDEX="{l.index}">
<MvDIMENSION INDEX="{l.index}">
</MvASSIGNARRAY>

Another variation is:

<MvASSIGN NAME = "l.fruit:apple" INDEX="{l.index}" VALUE = "{ l.value }" >

This would lead to l.fruit:apple[index]

It is not possible to assign an array index like this. So this DOES NOT work:


<MvASSIGN NAME = "l.fruit[l.index]" MEMBER="{l.member}" VALUE = "{ l.value }" >

You can find the terms and usage in the docs (Mivascript reference manual), even though the number of examples is rather low.

Hope this helps.

Markus

truXoft
02-20-07, 10:15 PM
It is not possible to assign an array index like this. So this DOES NOT work:

<MvASSIGN NAME = "l.fruit[l.index]" MEMBER="{l.member}" VALUE = "{ l.value }" >
Actually, it is possible to assign an array index in this way too, Markus. You just have a syntax error in your example. You can assign arrays in any way you can imagine, in MIVA Script. You can use arrays of structures, structures of arrays, structures of structures, multidimenional arrays of structures, or arrays of structures of arrays, etc. You just need to be persistant in the chosen way, and cannot access it once as array of structures, and then as structure of arrays.

So if you chose to use the syntax as in your example, you either use constants:
<MvASSIGN NAME="l.fruit[21]" MEMBER="{l.member}" VALUE="{l.value}">
or, in case you need to pass a variable value to the index, you have to use this syntax:
<MvASSIGN NAME="{'l.fruit[' $ l.index $ ']'}" MEMBER="{l.member}" VALUE="{l.value}">

or

<MvASSIGN NAME="{'l.fruit[' $ l.index $ ']:' $ l.member}" VALUE="{l.value}">

RayYates
02-22-07, 10:11 PM
Instead of structures, isn't this also valid as a multi dim array?

<MvASSIGN NAME="l.variable[l.x][l.y]" VALUE="{ expression }">

The advantage is that you can loop through either dimension.

truXoft
02-22-07, 11:54 PM
Although similar, arrays are not idetical to structures, and neither multidimensional arrays are. Of course, in MIVA Script you can use mutlidimensional arrays (or multidimensional arrays of structures, or structures of multiminensional arrays, or multidimensional structures of multidimensional arrays, etc.), but the syntax you show is still wrong. When using plain assignment, you either need to use constants (in plain double-quotes), or an expression (in curly brackets) in the variable name, exactly as shown in my previous post.

You can use the syntax you show when you use array names in an expression (the right side of the assignment) without any problems (because it is already being evaluated by the parser, since being in curly braces):

<MvASSIGN NAME="l.var" VALUE="{l.variable[l.x][l.y] + 20}">

There is still an important difference between both cases, though: in the first case the expression must avaluate to a constant string representing the variable name (or array/structure memebr name), while in the second case the expression evaluates not into the name, but into the value of the variable (or array/structure memebr name).

RayYates
02-23-07, 07:14 AM
Yes. Thanks Ivo for clarifying that. I understand, but its not exacly an intuitive syntax is it? It seems it would be rather simple for the MivaScript language developers to have made the parser understand the simple syntax name="z[x][y]" or "z[x,y]". After all if it understands z as a variable name how hard would it be to resolve[x] as an index variable name?

Alas, I am drifting from the thread topic.