Sorry this blog has been neglected… Facebook makes things much easier to share…
In some other….. ‘proper’… coding languages you have a function called Enumerate…. this allows you to iterate through an array and not only get the value but also index of the item too….. like this… Who even uses Python??
In a traditional maxscript loop you’d need to use the index of the array….
myArray = #(“a”,”b”,”c”,”d”)
for i = 1 to myArray.count do
(
format “#%: %\n” i myArray[i]
)
or you had to put a counter outside of the array
myCounter = 0
for o in myArray do
(
myCounter += 1
format “#%: %\n” myCounter o
)
But we can enumerate if we create a function to do this for us.. I wrote this one which uses the rarely used DataPair value type…. DataPair Value Type from Maxscript Help
Data pairs are actually quite useful, especially as you can label the values… so instead of reading o.v1 and o.v2 we can read o.index and o.value… a much more pleasant way to read our code…
fn enumerate ar = for i = 1 to ar.count collect (DataPair index:i value:ar[i])
myArray = #(“a”,”b”,”c”,”d”)
for o in enumerate(myArray) do
(
format “#%, %\n” o.index o.value
)