Search an array and output possition

Hi,
Is there a way to filter an array, and output the position for the first match?

Thanks

It is hard to tell what you are trying to accomplish though but i suspect if you really need the index there is probably a better way.

Depending on what you are trying to do one or more of these can help.

myList.sortBy[lastUpdate] // sort a list of items by lastUpdate
myList.filter[i|i.state == ON] // get those items that are on
myList.forEach[i|i.sendCommand(ON)] // send ON to all items in the list 

Perhaps if you posted more details about what you are trying to do.

Sorry…
I have 2 arraylists, with corresponding values (1 value belongs to the 1 value of the other array), so I need a way to search the first array for a value, to find the corresponding value in the second array…

G

I still am guessing that there is a better way to do this than two arrays or even better than what I’m about to suggest.

Unfortunately there really isn’t a way that I know of to loop through one array to get the index for the value you are looking for. However, have you considered using a hashMap? Then you can use the name or some other aspect of the object in the one arrayList as the key to pull the corresponding item out of the hashMap. For example, I use hashMaps to keep track of Timers that I have set that are related to a given Item using the Item’s name as the key and the Timer as the value. Then when a rule triggers on an arbitrary Item I can look up its Timer using the Item’s name.

That being said, I’ve found that any time I see this sort of coding pattern in the Rules DSL it is a code smell. This approach in the DSL results in rules that are far more complex, far longer, and far more difficult to maintain than a more Rules DSL appropriate approach.

For example, when I have need to get access to one Item that corresponds to another Item I use the approach documented here.. The short version is make as much as possible be Items rather than global variables in your rules files. Put your Items in groups and when Items correspond to each other use a naming scheme that makes it easy to construct the name of the corresponding Item using the name of the Item you have access to. Then I use gMyGroup.filter[i|i.name = name].head to get the corresponding Item.

It works well and in my case it let me do the same thing in less than 50% of lines of code. For example, I was able to drop my lighting rules from 1200 lines of code using lists and hash maps down to 350 lines of code.

Thanks,
That worked great!

May I ask which approach you chose: hashMap or Groups? Its mainly just idle curiosity. If you did switch to Groups, did it simplify or reduce your lines of code at all?

I used HashMap. I have never used that before, but its perfect for what I’m trying to accomplice.

Thanks.