|
Defines a multi-dimension array or structure using
Arrays and structures are used to store multiple values in a single variable. They can consist of a simple indexed list of values or more complex spreadsheet or database like structures. If you think of a single dimension array as an simple numbered list, a two dimension array can be though of as a spreadsheet with numbered rows and columns. Miva Script provides flexible tools for creating even more complex data structures. Arrays can also be assigned using <MvASSIGN>; often the resulting code is shorter, but their is no functional difference between the two forms. Attributes
No other tags than <MvDIMENSION> or <MvMEMBER> are allowed between <MvASSIGNARRAY> and </MvASSIGNARRAY>. Miva Script arrays are fully multi-dimensional (up to the limits of the system). If a value is assigned to an index or member that did not previously exist, the entry will be created. If a value is assigned to an array without specifying an index, the array will be destroyed and the variable will revert to a simple variable. Likewise, if a value is assigned to an element that previously contained more dimensions, the values in the extra dimensions will be destroyed and replaced with the single value. Two Dimensional ArrayAn tabular representation the array declared below could look like this.
Example:<MvASSIGNARRAY NAME = "l.coordinates" VALUE = "{ 100 }"> <MvDIMENSION INDEX = "1"> <MvDIMENSION INDEX = "1"> </MvASSIGNARRAY> <MvASSIGNARRAY NAME = "l.coordinates" VALUE = "{ 200 }"> <MvDIMENSION INDEX = "1"> <MvDIMENSION INDEX = "2"> </MvASSIGNARRAY> <MvASSIGNARRAY NAME = "l.coordinates" VALUE = "{ 300 }"> <MvDIMENSION INDEX = "2"> <MvDIMENSION INDEX = "1"> </MvASSIGNARRAY> <MvASSIGNARRAY NAME = "l.coordinates" VALUE = "{ 400 }"> <MvDIMENSION INDEX = "2"> <MvDIMENSION INDEX = "2"> </MvASSIGNARRAY> <MvEVAL EXPR="{ l.coordinates[2][1] }"><br> result: 300 Structured ArraysIt's often more convenient to refer to tabular data by name rather than number. Structures are name based collections of variables.
Example:<MvASSIGNARRAY NAME = "l.agents" VALUE = "Fox"> <MvDIMENSION INDEX = "1"> <MvMEMBER NAME = "First"> </MvASSIGNARRAY> <MvASSIGNARRAY NAME = "l.agents" VALUE = "Mulder"> <MvDIMENSION INDEX = "1"> <MvMEMBER NAME = "Last"> </MvASSIGNARRAY> <MvASSIGNARRAY NAME = "l.agents" VALUE = "Special Agent"> <MvDIMENSION INDEX = "1"> <MvMEMBER NAME = "Title"> </MvASSIGNARRAY> and so on. <MvEVAL EXPR="{ l.agents[3]:title }"><br> result: Assistant Director EvaluationArray elements can be accessed by specifying an array index in square brackets. "{ l.array[n] }" Members are specified using the colon as a prefix. { l.email:subject } If an array is evaluated with no specified index, Miva Script will return a string in the comma separated list e.g. "value,value...", with sub-arrays at a particular index being displayed as a value delimited by parenthesis. Example:<MvEVAL EXPR="{ l.coordinates }"> resuts: 100,200,300,400 <MvEVAL EXPR="{ l.agents }"><br> results: Fox,Mulder,Special Agent etc. While LoopsLooping through arrays inside <MvWHILE> tags, either for assigning values or displaying results, is where they show their true power. This example illustrates several important techniques at once. Study it carefully. The variables g.names and g.abbr contain lists of literal data. Frequently we deal with data from a database or external files that we want to read into an array or structure. NOTE: Loops are not limited to positive increments. You can display your array in reverse order if desired. Create the Array:<MvCOMMENT> Create a dataset <MvCOMMENT> <MvASSIGN NAME="g.names" VALUE="January,February,March,April,May,June,July,August,September,October,November,December"> <MvASSIGN NAME="g.abbr" VALUE="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"> <MvCOMMENT> Convert the data to an array structure. <MvCOMMENT> <MvASSIGN NAME="g.posn" VALUE="{ 1 }"> <MvWHILE EXPR="{ g.posn LE 12 }"> <MvASSIGN NAME="g.month" INDEX="{ g.posn }" MEMBER="name" VALUE="{ gettoken(g.names,',',g.posn) }"> <MvASSIGN NAME="g.month" INDEX="{ g.posn }" MEMBER="abbr" VALUE="{ gettoken(g.abbr,',',g.posn) }"> <MvASSIGN NAME="g.month" INDEX="{ g.posn }" MEMBER="first_date" VALUE="{ g.month[g.posn]:name $ ' 1, ' $ s.tm_year }"> <MvCOMMENT> Next item <MvCOMMENT> <MvASSIGN NAME="g.posn" VALUE="{ g.posn + 1 }"> </MvWHILE> NOTE: the first_date member contains the formatted date for each month(e.g. January 1, 2010 Once loaded, loop through the array again to display it. In this example we populate an HTML <select> dropdown. box. Output the Array:<select name="Starting_Month"> <MvASSIGN NAME="g.posn" VALUE="{ 1 }"> <MvWHILE EXPR="{ g.posn LE 12 }"> <option value="{ g.month[g.posn]:abbr }"><MvEVAL EXPR="{ g.month[g.posn]:first_date }"></option> <MvASSIGN NAME="g.posn" VALUE="{ g.posn + 1 }"> </MvWHILE> </select> Results: Arrays in HTML FormsThe interaction between a web page and Miva Script is most often through an HTML form. Embedded arrays can simplify form processing. Example:<form method="post" action="color_options.mvc"> <input type="hidden" name="option[1]" value="red"> <input type="hidden" name="option[2]" value="orange"> <input type="hidden" name="option[3]" value="yellow"> <input type="hidden" name="option[4]" value="green"> <input type="hidden" name="option[5]" value="blue"> <input type="hidden" name="option[6]" value="magenta"> <input type="hidden" name="option[7]" value="violet"> </form> When submitted to a Miva Script program, it will receive the array as g.options[n] that can be processed in a While loop. |