Lesson 15: Enumeration Stations….

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
)

Advertisements

About davewortley

Somewhere between an artist and a programmer, I like technical things, but being creative. I love problem solving and coming up with elaborate solutions. This blog is where I shall share ideas, tips, tutorials and anything that amuses me.
This entry was posted in 3dsmax, Lessons, MaxScript. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s