Difference between revisions of "Functions that create arrays"
Line 46: | Line 46: | ||
Definition viewed as a table: | Definition viewed as a table: | ||
− | {| | + | {|class=''wikitable'' |
− | ! | + | ! !! colspan="5" style="text-align: left;" | Years ▶ |
− | ! colspan="5" |Years | ||
|- | |- | ||
− | !'''Car_type | + | ! style="width:100px;" |'''Car_type ▼ ''' |
− | !'''2005''' | + | ! style="width:75px;" |'''2005 ''' |
− | !'''2006''' | + | ! style="width:75px;" |'''2006 ''' |
− | !'''2007''' | + | ! style="width:75px;" |'''2007 ''' |
− | !'''2008''' | + | ! style="width:75px;" |'''2008 ''' |
− | !'''2009''' | + | ! style="width:75px;" |'''2009 ''' |
|- | |- | ||
− | !'''VW''' | + | !'''VW''' |
− | |8000 | + | | 8000 |
− | |7000 | + | | 7000 |
− | |10K | + | | 10K |
− | |6000 | + | | 6000 |
− | |9000 | + | | 9000 |
|- | |- | ||
− | !'''Honda''' | + | !'''Honda ''' |
− | |10K | + | | 10K |
− | |12K | + | | 12K |
− | |11K | + | | 11K |
− | |14K | + | | 14K |
− | |13K | + | | 13K |
|- | |- | ||
− | !'''BMW''' | + | !'''BMW''' |
− | |5000 | + | | 5000 |
− | |8000 | + | | 8000 |
− | |8000 | + | | 8000 |
− | |7000 | + | | 7000 |
− | |10K | + | | 10K |
|} | |} | ||
+ | |||
The size of each array in square brackets must match the size of the corresponding index. In this case, there is an array of three elements (for the three car types), and each element is an array of four elements (for the four years). An error message displays if these sizes don’t match. See also | The size of each array in square brackets must match the size of the corresponding index. In this case, there is an array of three elements (for the three car types), and each element is an array of four elements (for the four years). An error message displays if these sizes don’t match. See also | ||
“[[Functions that create arrays#Size(u,listLen)|Size(u,listLen)]]”. | “[[Functions that create arrays#Size(u,listLen)|Size(u,listLen)]]”. | ||
Line 90: | Line 90: | ||
Definition viewed as a table: | Definition viewed as a table: | ||
− | {| | + | {|class=''wikitable'' |
− | ! | + | ! !! colspan="5" style="text-align: left;" | Years ▶ |
− | ! colspan="5" |Years | ||
|- | |- | ||
− | !'''Car_type | + | ! style="width:100px;" |'''Car_type ▼ ''' |
− | !'''2005''' | + | ! style="width:75px;" |'''2005 ''' |
− | !'''2006''' | + | ! style="width:75px;" |'''2006 ''' |
− | !'''2007''' | + | ! style="width:75px;" |'''2007 ''' |
− | !'''2008''' | + | ! style="width:75px;" |'''2008 ''' |
− | !'''2009''' | + | ! style="width:75px;" |'''2009 ''' |
|- | |- | ||
− | !'''VW''' | + | !'''VW''' |
− | |8000 | + | | 8000 |
− | |7000 | + | | 7000 |
− | |10K | + | | 10K |
− | |6000 | + | | 6000 |
− | |9000 | + | | 9000 |
|- | |- | ||
− | !'''Honda''' | + | !'''Honda ''' |
− | |13K | + | | 13K |
− | |13K | + | | 13K |
− | |13K | + | | 13K |
− | |13K | + | | 13K |
− | |13K | + | | 13K |
|- | |- | ||
− | !'''BMW''' | + | !'''BMW''' |
− | |5000 | + | | 5000 |
− | |8000 | + | | 8000 |
− | |8000 | + | | 8000 |
− | |7000 | + | | 7000 |
− | |10K | + | | 10K |
|} | |} | ||
'''Example 4''' | '''Example 4''' | ||
Line 142: | Line 141: | ||
|18 | |18 | ||
|} | |} | ||
+ | |||
<tip title="Tip">There are some significant disadvantages to using the '''Array()''' function to change the index of an array in the fashion demonstrated in Example 4. Specifically, if a second dimension were later added to '''<code>Table_a</code>''', the index that the '''Array()''' function changes might not be the one you intended. The preferred method for changing the index, which does fully generalize when '''<code>Table_a</code>''' has many dimensions, is to use the slice operator (see Tip on re-indexing) as follows: | <tip title="Tip">There are some significant disadvantages to using the '''Array()''' function to change the index of an array in the fashion demonstrated in Example 4. Specifically, if a second dimension were later added to '''<code>Table_a</code>''', the index that the '''Array()''' function changes might not be the one you intended. The preferred method for changing the index, which does fully generalize when '''<code>Table_a</code>''' has many dimensions, is to use the slice operator (see Tip on re-indexing) as follows: | ||
'''<code>Table_a [ @Car_type = @car_model ]</code>'''</Tip> | '''<code>Table_a [ @Car_type = @car_model ]</code>'''</Tip> |
Revision as of 02:17, 11 December 2015
Usually, the most convenient way to create an array of numbers or text values is as an edit table. When viewing the definition of the variable, choose Table from the expr menu to create an edit table (see Defining a variable as an edit table). If you want to define a table by explicitly listing its indexes and providing expressions to generate its values or sub-arrays, you might find Array() more convenient.
If you select expr from the expr menu, it displays it as a table expression in the Definition field (rather than a separate edit table), listing the indexes and values.
Array(i1, i2, … in, a)
Assigns a set of indexes, i1, i2, … in, as the indexes of the array a, with i1 as the index of the outermost dimension (changing least rapidly), i2 as the second outermost, and so on. a is an expression returning an array which typically has at least n dimensions, each dimension with the number of elements matching the corresponding index. You can use array to change the index variable(s) from one to another with the same number(s) of elements. Array() is one of the few places where you actually need to worry about the order of the indexes in the array representation.
Use Array() to specify an array directly as an expression. Array() is similar to Table(); in addition, it lets you define an array with repeated values (see Example 3), and change indexes of a previously defined array (see Example 4).
Library: Array
Example 1
Definition viewed as an expression:
Index Car_type := ['VW', 'Honda', 'BMW']
Array(Car_type, [32, 34, 18])
Definition viewed as a table:
VW | Honda | BMW |
---|---|---|
32 | 34 | 18 |
Example 2
If an array has multiple dimensions, then the elements are listed in nested brackets, following the structure of the array as an array of arrays (of arrays..., and so on, according to the number of dimensions).
Definition viewed as an expression:
Array(Car_type, Years, [[8K,7K,10K,6K,9K], [10K,12K,11K,14K,13k], [5K,8K,8K,7K,10k]])
Definition viewed as a table:
Years ▶ | |||||
---|---|---|---|---|---|
Car_type ▼ | 2005 | 2006 | 2007 | 2008 | 2009 |
VW | 8000 | 7000 | 10K | 6000 | 9000 |
Honda | 10K | 12K | 11K | 14K | 13K |
BMW | 5000 | 8000 | 8000 | 7000 | 10K |
The size of each array in square brackets must match the size of the corresponding index. In this case, there is an array of three elements (for the three car types), and each element is an array of four elements (for the four years). An error message displays if these sizes don’t match. See also “Size(u,listLen)”.
Example 3
If an element is a scalar where an array is expected, Array() expands it to create an array with the scalar value repeated across a dimension.
Definition viewed as an expression:
Array(Car_type, Years, [[8K,7K,10K,6K,9K], 13K, [5K,8K,8K,7K,10k]])
Definition viewed as a table:
Years ▶ | |||||
---|---|---|---|---|---|
Car_type ▼ | 2005 | 2006 | 2007 | 2008 | 2009 |
VW | 8000 | 7000 | 10K | 6000 | 9000 |
Honda | 13K | 13K | 13K | 13K | 13K |
BMW | 5000 | 8000 | 8000 | 7000 | 10K |
Example 4
Use Array() to change an index of a previously defined array.
Index Car_model := ['Jetta', 'Accord', '320']
Variable Table_a:= Table(Car_type) (32, 34, 18)
Variable Table_b:= Array(Car_model, Table_a) →
Jetta | Accord | 320 |
---|---|---|
32 | 34 | 18 |
Table_a
, the index that the Array() function changes might not be the one you intended. The preferred method for changing the index, which does fully generalize when Table_a
has many dimensions, is to use the slice operator (see Tip on re-indexing) as follows:
Table_a [ @Car_type = @car_model ]
Enable comment auto-refresher