Dynamo: difference between IndexOf and FirstIndexOf

Both IndexOf and FirstIndexOf will return the index of the first occurrence of a given value.
In the next example we are using both nodes to find in a list the first value “3” and to return it’s index which, in this case, gives us the index “2”.

Index-Basic

At first glance, both nodes are doing the same. So what is the difference???

Well, the big difference is the data structure the “Item” input port of the FirstIndexOf node and the Element input port of the IndexOf node is expecting.
The FirstIndexOf node is just asking for one value (“var”) while IndexOf is supporting any data structure (“var[]..[]”). So for the latter you can use one value or a list of values.

Index-Inputs

This means that the difference between both nodes becomes clear when you use a list as the input for the “Item” or “Element” input ports.

Because the “Item” port of FirstIndexOf is just operating on 1 value, if you input a list with multiple values, Dynamo will automatically execute the node as many times as there are values in the list (replication).
In the example below this means that FirstIndexOf will be run a first time to look for the index of the first occurrence of the value “1” (result is index “0”). Then Dynamo will run the same node a second time to look for the index of the first occurrence of value “2” (result is index 2), and so on.
If the value cannot be found he will return “-1”. This is the case for value “3” which does not exist on the main level of our input list (it exists only in a sublist).
(If you want to search for a value in the sublists you need to use the levels of the input port, but that is another story.)

Index-FIO

Thus, FirstIndexOf will NOT look if the complete list {1,2,3} exists as a sublist in the given input data. Again, the node will be run for each element in the list {1,2,3}.

On the other hand, the “Item” input port of IndexOf can handle any data structure, so also lists.
This means that this node will only be run once and will use all the given data to look for its first index. So IndexOf will really look if the list {1,2,3} exists as a sublist and will return the index of the first occurrence.

Index-IO

 

 


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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s