< Back to IRCAM Forum

Returning index of nested list items

Hi all,

I am looking to perform a lookup function to find the position of a list within another list. The depth of the lists will never increase. For example, it will always be ((aa)(ab)(ac)).

Is it possible to pass a variable to a function and have it return the corresponding index of that item in the list?

I’ve attached my attempt below, but OM evaluated to nil. I presume this is due to the nested list. I can successfully utilize the ‘position’ function without nested lists.

Thank you :slight_smile:

Dear apoorbaugh,

You are nearly there. The problem you have is that position’s test default to eq which test only atoms. For list or any other data you should use equal as so:
Screenshot_2023-01-04_22-46-58

In order to have the extra input (which is a key and not an optional arg) you should press k and choose test in the menu.

Best
K

Karim,

Thank you! I knew that optional arguments were the key. My mistake, I was pressing SHIFT+K to try and add the inlet to no avail. Is there a way (not that it’s much faster) to instantiate objects with optional variables during creation?

something like [position 1 1 test:equal]?

Yes. If I understand you well, you want to create the function with the ready made inputs arguments. So in this case you just type (without the brackets):
[position (0 3 7) ((0 2 6) (0 3 7) (7 9 11)) :test equal]
and there you have your function with its arguments and extra key inputs.
By the way, shift+k is to remove the extra key input.

Best
K
PS: and thank you for the tip you’ve posted. Very precious to windows users.

1 Like

It is nice to give something back to the community, especially after you all have been so generous with your time, patience, and knowledge toward me!

Your assistance and knowledge is appreciated as always, Karim.

1 Like