Any fast way to populate an array?


1) Give a description of the problem
I’m fiddling with my arrive/leave piston. What I’d like is to have it send a “Welcome home, (name here)” message

2) What is the expected behaviour?
I’ve already got the piston working, but I appear to have to populate my array of names manually, one at a time

3) What is happening/not happening?
Is there any way to populate the array in one line? There doesn’t appear to be a way to make it a constant and/or define it in the define section.


just use string rather than string[] and then separate the list with commas like you did for the phone numbers. arrayItem() will work fine. indexOf() won’t work properly. depends how you want to use the array.


Wow. I had no idea (and didn’t even consider the possibility) that arrayItem() would work on a text string that way. Thanks. That solves the problem. I could probably treat the phone numbers the same way and not have to resort to my mid() function. Thanks.
I’m still curious how to populate an array, but it’s not a salient concern now.


It was a lesson for me also a while back. You would thing the array variable types would be useful but populating them is tedious. I haven’t seen or read of any simple way to do it.


I realize this will not help the original post, but depending on the source of the data, it can be quite simple.

Here is a sample from one of my old WUnderground pistons.

    integer i; (not set)
    string[] a; (not set)
end define


end execute

For clarification, a[] is a array with 70 data points, and the code above writes to a{24] thru a[69].
(I have other code writing to the first 24 slots)


Yes. That confirms that there doesn’t seem to be a way to populate an array “all at once”. You have to do it one element at a time. In your example, it doesn’t make sense to try to do it any other way and, as you say, it’s simple to do and simple to figure out when reading it over in a year.
It would be nice, however, if there was a way to populate an array of constants at the define stage.
Something like:
string[] fourNames = [Reed, Susan, Ben, Johnny];
end define
Although, with arrayItem[] working as it does on comma separated strings, I can’t see that I really need it. I suppose one could even put numerical values in and then cast the result element to an integer or decimal if needed.
eg: string tempSettings=‘0,40,70,90’;


Wellllllll… That’s not entirely true. It can be done in a single line, as seen below.
(ignore the highlighted region)

Unfortunately, I think it takes more work to code it this way…
(and it’s easier to mistype the syntax)


Agreed. (Though the pedantic part of me says that even webCoRE says that’s four lines :slight_smile: ) If I had to do it, that’d be the way I’d do it, but since my primary use for wanting to pre-load a list is a list of strings, the comma delimited single string works well and can be done as a constant in the define.

“Unfortunately, I think it takes more work to code it this way…”

Yes, and that goes against my “enlightened laziness” nature.


I agree. The main reason I even mentioned it is for others finding this thread later…

I guess, the philosophy I usually adhere to when coding is, I would rather more text/lines, IF it makes the piston easier to read and/or edit… or perhaps more importantly, if it executes smoother.

I rarely condense multiple lines into one just for the sake of less “bytes”.
(we live in a terabyte world, and each piston is less than 0.1 megabytes)


Just for the record: I created the “epmk” piston above just to see if it could be done in one line, but I normally would populate the array like this


I use the drag & drop method to copy the “Alex” line 3 times. This makes editing quick & easy.

Once I have pressed TEST, the array gets populated. At that point, I will often Disable that one block, since those types of variables only change a couple of times a year. (that block doesn’t need to run at each trigger) This also makes future edits a breeze. If I want to add or change any of the current variables, I just Enable that block again.


Curiosity got the best of me… so I had to run a few tests.

It seems that when comparing “looping on one line

versus “four individual commands”

the loop method takes up more than double of the allowed space.

Real life testing using the two snippets above:

Loop    X 10 = 13 'chunks' of data
No Loop X 10 =  6 'chunks' of data

I think I will continue using the four individual lines to conserve space.
(in this case, the “pretty” code also takes up the least amount of space)


" I would rather more text/lines, IF it makes the piston easier to read and/or edit… or perhaps more importantly, if it executes smoother."

Exactly. “Cute code” can be fun to write, but it’s not at all fun to try to read when you haven’t looked at it for a few months.

Once I have pressed TEST, the array gets populated

I had thought a while back that having a non-triggered “initialize variables” piston could come in handy for situations like that, but that would require using globals. Disabling a block in the code means you can keep things local. I like that.