Reputation: 881
Is there an extension to Netlogo that allows the creation of multi-dimensional arrays accessed with an "array[i,j]" style notation? I find that using item leads to long and difficult to read code, and I just get lost with anything over 2D lists (though I realise others are perfectly comfortable with it).
Thank you!
Upvotes: 1
Views: 1543
Reputation: 41
Now there is an extension to netlogo that called : array
Here is an example of code :
extensions [array ]
to setup
set ex array:from-list n-values 10 [0]
set i 1
while [i < 10 ][
array:set ex i i + 1
]
end
Upvotes: 2
Reputation: 12580
Not that I know of, but it's pretty easy to make a primitive to do this yourself. Here's a couple different implementations to give you ideas:
Using recursion:
to-report nested-get [ lst indices ]
ifelse empty? indices [
report lst
] [
report nested-get (item first indices) (but-first indices)
]
end
Using reduce:
to-report nested-get [ lst indices ]
report reduce [ [ l i ] -> item i l ] fput lst indices
end
I chose to put the indices after the list in the arguments to better match the syntax you were hoping for, but you might consider putting them before, to correspond with item
. The reporters are invoked as follows:
observer> show nested-get [ [ "a" "b" "c" ] [ "d" "e" "f" ] ] [1 2]
observer: "f"
Unfortunately, if you want to use variables or reporters as your indices, you'll need to use list
instead of []
:
observer> let i 0 let j 1 show nested-get [ [ "a" "b" "c" ] [ "d" "e" "f" ] ] (list i j)
observer: "b"
The syntax supports an arbitrary amount of nesting. You could also make dimension specific versions to make the syntax simpler for common cases:
to-report item2 [ i j lst ]
report item j item i lst
end
Upvotes: 4