Arrays
Arrays are containers of FISH variables where the inserted items are assigned and retrieved via an integer index. Arrays may be multidimensional. See Array Utilities for available methods.
Arrays are less flexible and perform less well than Lists or Maps. If very large amounts of data needs to be stored in multiple dimensions then array types may be the optimal choice, otherwise we suggest arrays be replaced with Lists whenever possible in older FISH code.
Arrays may be created using the array.create
library function.
The number of arguments give to this function indicate the array dimension, and the value of those arguments indicate the size in that dimension.
There is no limit to the number and size of the array dimensions, except memory capacity.
Array objects can be destroyed with the array.delete
intrinsic function.
It is not necessary to do this, array objects that are not referred to by any fish value will be automatically destroyed and their memory recovered.
Members of an array are FISH variables (except that there is no name associated with them), and are therefore governed by the same rules as symbol values. If a FISH symbol points to an array, array elements may be accessed by specifying an index in a parenthesis-enclosed list. For example:
var1 = abc(3, nn + 3, math.max(5, 6))
is a valid statement if abc is currently pointing to a three-dimensional array. Arrays may appear on both sides of an assignment, and arrays may be used as indices of other arrays.
Be aware that if abc currently points to an array, the statement:
abc = 0
will cause abc to become an integer whose value is zero. If no other symbol points to the array, all access to that array is lost and the array will be deleted automatically.
model random 10001
fish define afill ; fill matrix with random numbers
;; Method one, using array.create and loops
var = array.create(4,3)
loop local m (1,array.size(var,1))
loop local n (1,array.size(var,2))
var(m,n) = math.random.uniform
end_loop
end_loop
;; Method 2, using lists
var2 = array(math.random.uniform(4*3),4,3)
end
fish define ashow(v) ; display contents of matrix
loop local m (1,array.size(v,1))
local hed = ' '
local msg = ' '+string(m)
loop local n (1,array.size(v,2))
hed = hed + ' '+string(n)
msg = msg + ' '+string(v(m,n),8,' ',8,'e')
end_loop
if m = 1
io.out(hed)
end_if
io.out(msg)
end_loop
end
[afill]
[ashow(var)]
[ashow(var2)]
Arrays can be compared against each other with ==, <, > etc. If the structures are of the same type name then each member is compared in order.
Arrays are an iterable type. This means you can use it as the target of a loop foreach statement (see Loop ForEach and can be split (see Splitting). Each entry of the array is iterated with the left-most index changing first and the right-most index changing last.
As with most aggregate types, when the value is listed it will simply indicate that it is of type Array, give the size, and an unique ID number assigned to it. To see the contents of the array use the fish list contents
command. Be aware that for large multi-dimensional arrays this could be a considerable amount of data.
Was this helpful? ... | Itasca Software © 2024, Itasca | Updated: Dec 19, 2024 |