Correct Way to Do Array?


#1

Trying to use an array to simplify how I would do this, but I probably l am just using it incorrectly. I could just have separate left/right variables throughout but this seems a little cleaner.

But I get an “Array Item Index is Out of Bounds” error for this (I get the error when I’m in the UI creating the command, not at runtime):

  arrayitem(2,doorColors)

Is there a correct way to do this?

Note: the piston is a work in progress and wouldn’t quite work right now I don’t think. I’d like to figure this out thing as a learning step before I wrap it up.

Thanks in advance!


#2

I think to use the Array Item, your variable needs to be the List types. Those variables can’t be populated manually and would need a command in the piston to populate them.

I’ll take a crack at updating it for you in a minute - difficult to describe but one you see it you’ll be able to figure out how it works.


#3

This is how to do what you’re after using arrays. I’ve put two methods in here for populating it… a loop or manually listing out. Obviously loops become easier once you get a longer list. Another thing you can do is just set a list of all colors you want to use, and then when the holiday is here you just select the colors you want. For example, if you had Red Green and Orange listed (0, 1, and 2 in your array)… you could just choose 1 and 2 for Christmas, 2 and 3 for Halloween.

Here’s the snapshot - if this doesn’t work or make sense, let me know and I’ll try to help more! I don’t have RGB bulbs to test but I could throw something together using an array in some other way to prove it out.


Any fast way to populate an array?
#4

To add to the response from @michicago, arrayItem expects the index to start at zero rather than one, so your first item is arrayItem(0, doorColors). This function actually operates on any comma separated string so your variables should work fine.

The existing arrayItem, count, and similar functions that work on comma separated strings should not be used (or perhaps with great caution) with the array data types. Those arrays are converted to a string, so if any value contains a comma it will be split into two indexes for arrayItem. Additionally, if any of the array keys are non numeric arrayItem will return key:value formatted items. Plenty more on that here


#5

I’ll have to tinker with that more. When I first imported his piston, arrayItem(0, christmasColors) returned an error… I thought you had to use a list for it to work.


#6

Thanks, fellas. That got me there.

I can’t explain it, but I had some initial issues with the array index being zero, but it’s working now. There’s probably more sophisticated ways to handle some of this but it shouldn’t be updated often and for now, I’ll call it my first working piston–it can go from here. Your help…helped!