Array 2D for App Inventor
Table of contents:
- 1-Introduction
- 2-Create blocs
- 3-GetItem, SetItem
- 4-RowsCount, ColsCount
- 5-Convert array
- 6-Extract sub array
- 7-Redim
- 8-Transpose
- 9-InsRows, InsCols
- 10-ToString, ToStringEx
- 11-DelRows, DelCols
- 12-ShiftRows, ShiftCols
- 13-SwapRows, SwapCols
- 14-SortRowsOnCol, SortColsOnRows
- 15-Search, Replace
- 16-Shuffle
- 17-JoinArray
- 18-Copy, IsEqual, IsEmpty
- 19-Download extension and examples
1-Introduction
The extension makes it easier to work with 2D arrays by providing creation methods and numerous processing blocks.
Compatibility with app inventor is perfect: the extension's methods work with a list of lists (the rows of array) provided they are of the same length.
Exemple - array 3x2 (3 rows and 2 columns)
Other methods allow you to create an array after declaring it as an empty list type:
They are presented below.
2-Create blocs
The extension supports creating an array from text in CSV format, like:
"États-Unis,25.5\nChine,18.0\nJapon,4.2"
With the following rules:
- no trailing \n otherwise a final empty line is created
- the separator , is allowed in a field provided that the field is enclosed by "
- the separator \n is allowed in a field provided that the field is enclosed by "
- the character " is allowed in a field provided that they appear in pairs
- a field can be empty.
Example - a row of array (the last field is empty):
"Jean\n dit "Jeannot le fou" " , "12,5" , \n ...
`textBrackets` is a string like:
"[[ US, 25.5], [ Chine, 18.0], [Japon , 4.2]]"
The bloc return an array.
The fields are random integer between intMin an intMax inclusive.
All the fiels have same value (number, string, boolean, list,....).
This method split `list1D` in rows of length is rowsCount.
The list size must be divisible by `rowsCount`.
In particular, if rowsCount=1, the returned 2D array will have only one row and if rountCount=size of list1D, the returned array will have only one column.
Useful for transforming a row-vector or column-vector to array.
3-GetItem and SetItem
GetItem return the value at the (rowIndex, colIndex) position. Indexes start at 1.
SetItem return a new array with the given `value` at (rowIndex, colIndex) position. Indexes start at 1.
4-RowsCount and ColsCount
RowsCount return the number of rows and ColsCount the number of columns.
5-ToStringCSV, ToStringBrackets, ToList1D
ToStringCSV convert array to string CSV format and return the string.
ToStringBrackets convert array to string brackets format and return the string.
ToList1D convert array to simple list by joining the rows and return the list.
6-ExtractSubArray
Extracts a sub-table between the rows `startRow` et `endRow` inclusive and between the columns `startCol` and `endCol` inclusive.
7-Redim
Resize the given array with the new dimensions. Return an array.
8-Transpose
Transpose the given array and return the new array. The rows of the given array become the columns of the new array and the colomns of given array become the rows of the new array.
9-InsRows, InsCols
InsRows insert rows into the given array. The insertion position is given by the fromRowIndex parameter and the rowsToIns is a given **array**.
10-ToString, ToStringEx
ToString visualizes the given array in the simplest possible way, in text mode.
ToStringEx visualizes the given array in a sophisticated way, in text mode but with row and/or column headers.
headRow,headCol are integers with signification below:
0.0 - no headers for rows or columns
1.0 - numbers rows by adding a first column
0.1 - numbers columns by adding a first row
1.1 - numbers rows and columns
2.0 - uses the first row of the array as the column header
0.2 - uses the first column as the row header
2.2 - both
2.1 - uses the first column as the row header and numbers the columns
1.2 - uses the first row as the column header and numbers the rows
rowsCountTodo, colsCountTodo limits the number of rows and columns from 1. (0,0 to display all the array).
align=-1 | 0 | 1 for left | middle | right to justify items in the column.
Note:
To display in color chart mode, use KevinKun's TableViewer extension by converting the array to a CSV string with ToStringCSV block.
11-DelRows, DelCols
DelRows delete rows at `startRowIndex` until `endRowIndex` inclusives.
DelCols delete columns at `startColIndex` until `endColIndex` inclusives.
12-ShiftRows, ShiftCols
ShiftRows shifts down (shift>0) or up (shift<0) the specified range of rows.
ShiftCols shifts right (shift>0) or left (shift<0) the specified range of colomns.
13-SwapRows, SwapCols
SwapRows swap two non-overlapping row ranges. The ranges can be of different heights.
SwapCols swap two non-overlapping columns ranges. The ranges can be of different weigths.
14-SortRowsOnCol, SortColsOnRow
SortRowsOnCol reorders the rows of range so that column `colIndex` is ordered in the specified order.
- alpha - true to sort alphabetically (10 < 2 and A < a), false to sort numerically (10 > 2). In the second case, non-comparable elements are grouped at the beginning of the range.
- ascending - true to place the smallest element on top and the largest at the bottom, false for the opposite.
- rowIndexMin, rowIndexMax delimit the range of rows on which to sort.
SortColsOnRow reorders the columns of range so that row `rowIndex` is ordered in the specified order.
15-Search, Replace
Search searches the array for items equal to value
ignoring case or not.
The pair rowIndex, colIndex can take the values:
- 0, 0 : search in full array
- rowIndex, 0 : search in rowIndex
- 0, colIndex : search in colIndex
- rowIndex, colIndex : Look if item at this position is equal at value.
If `ignoreCase` is true, the comparaison ignore the case, else the comparaison the is case sensitive.
The block return a 2D-array with 2 columns of locations (row, col) and empty if no match. To know to find out how many items matched, use RowsCount.
Replace searches the array for items equal to value
and replace by replace
.
The pair rowIndex-colIndex has same signification of Search.
The block return modified array.
16-Shuffle
Shuffle shuffles the items in the array (mode=0) or only the rows (mode=1) or only the columns (mode=2).
17-JoinArray
Join two arrays horizontally and return the new array.
Both arrays must have the same number of rows.
18-Copy, IsEmpty, IsEquals
Copy returns an array identical to the given array. Modifying one does not modify the other.
IsEmpty return true if RowsCount=0 and false else. If all the items of array are empty string, the array is not empty.
IsEquals return true if the two specified arrays have exactly the same elements in the same order with the same case.
19-Download extension and examples
Array.aix (21.2 KB)
Array2D.aia (36.2 KB)