Maps
Maps are similar to Arrays in that they store FISH variables in an ordered fashion. Unlike arrays, maps can be dynamically sized, and the key used to retrieve values from a map can either be an integer or a string. See the Map Utilities for available methods.
Maps can be created and assigned in one statement:
where the keys may either be integers or strings. Additional values can be added with the
map.add
method and removed with the map.remove
method. One can simply access values
by giving the map name followed by the key in parenthesis ().
The example below demonstrates the use of a map.
model new
fish define testBreak
loop local i (1,2)
testmap = map('first',1,'second',2,'third',3)
sum = 0
loop foreach local v testmap
sum = sum + v
endloop
endloop
end
@testBreak
[sum]
fish define test1
local temp = map('hello',1,'world',2)
test1 = temp('hello') + temp('world')
end
[test1]
fish define test2
testmap = map('first',1,'second',2,'third',3)
map.remove(testmap,'first')
map.add(testmap,'fourth',4)
testmap('second')=8
; values are now fourth->4, second->8, third->3
local output = 0
loop foreach n testmap
output = output + n
endloop
test2 = output
end
[test2]
fish define test3
; test keys
testmap = map('yet',1,'another',4,'test',9,'m',16)
testkeys = map.keys(testmap)
test3 = testkeys(3)
; check has
if (map.has(testmap,'wrongkey'))
hascheck = 909
else if (map.has(testmap,'another'))
hascheck = testmap['another']
endif
end
[test3]
[hascheck]
Was this helpful? ... | PFC 6.0 © 2019, Itasca | Updated: Nov 19, 2021 |