Is there a function to calculate the length of an array (list)?


#1

I see very little documentation on lists and how to work with them. I want to be able to run a for loop based on the length of a list that is dynamically changed. I also want to be able to remove all values stored in a list at once with one command or i could us a for loop again knowing the length of array would help to use this for loop to clear each value one by one.


#2

I know another workaround to this would be to define that a list will never be more than N and therefore create a for loop for 0 to N and then have a sub condition that checks each value within F[0] … F[N] for a non null value as an exit routine.


#3

You can run a For loop on each item in a device array. You can also use the count() function to return the number of items in an array.

More details needed, like a sample piston or actual application, before any solid answers or samples can be provided.


#4

Thanks!


#5

Try this for an example.

You can ignore the IFTTT trigger as this was just a way for me to manually trigger this piston to test my code, but i was playing around with the way webCoRE handles lists / arrays and how to get them to print out using a for loop with the length of the array as the final interval value of For loop.


#6

Yep, exactly right! If you run into any snags using arrays, just post up and someone will certainly help out!


#7

I am trying to figure out the length of an array to iterate in a for loop, but some of values are ‘0’ so count() returns the wrong count. It looks like for…each only works for devices.

I would also like to use nested arrays, but I have had no luck trying to get this to work.


#8

Not sure if we have size() available, been a while :grinning:


#9

Hi, I tried the count() function but its not considering empty value… do you know a workaround for this?


#10

I found it, size() works great!