Any fast way to populate an array?


#1

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.


#2

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.


#3

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.


#4

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.


#5

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.

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

execute


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)


#6

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:
define
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’;


#7

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)


#8

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.


#9

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)


#10

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

temp

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.


#11

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

It seems that when comparing “looping on one line
loopY

versus “four individual commands”
loopN

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)


#12

" 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.


#13

Hi all, bumping this topic from last year as I’m in a similar situation, however I am trying to populate an array of times instead. According to what I’ve read and experienced, I have to do a series of manual
Set Variable myArray[0] = value1
Set Variable myArray[1] = value2
Set Variable myArray[2] = valueN

Is there really to this date no way or workaround for setting an Array item with a variable index, i.e. Set Variable myArray[myIndex] = something?

Thanks,
/Max


#14

Arrays, or Lists, in WebCoRE are interesting objects. No, there is no simple way to populate an array. I think this is because the indexes are not even actually numbers. For example, you might want

   myArray[0]=value1
   myArray[1]=value2

and that works just fine. You can then reference myArray[0] or myArray[1] to get back the values you stored. You can also, however do this:

   myArray["green"]="Grass"
   myArray["blue"]="Sky"

In this case if you try to reference myArray[0] WC will have no idea what you are talking about. myArray[“green”] however will return “Grass”. So it is not a normal array and, therefore, cannot be populated in simple ways.


Set variable value for Sonos preset from smartthings
#15

Eloquently described, @guxdude:+1:

For what it’s worth, I am a fan of creating just a single line, IE:
Set Variable myArray[0] = value1
… and then Drag / Copy / Edit that line a few times.

It takes me about ten seconds per line, and then I disable the block afterwards, in case I ever need to initialize them again


#16

Huh, how about that… Thans for claryfying. So the index is actually more a mnemonic reference. Interesting. @guxdude It should be said I came across this posting: Correct Way to Do Array?
It would appear what’s being done in @michicago 's loop example is exactly what I need, however it does not work for a list of timestamps. Even if it supposedly works for a string array, it does however seem a bit over the top to convert back and forth between string and datetime formats just to process in an array. I guess I’ll have to bite the bullet and populate the array manually, bleh.


#17

Note that the loop example there is assigning data from one list to another list but the source list still needed to be populated manually at the beginning of the piston. Copying data can be simplified but you have to start somewhere. So, yes, bleh. :persevere:


#18

So, wait a sec - are you saying that if the array is manually pre-populated/initialized at piston start one can later set other values using a for-loop’s $index then? Copying data from one list to another is actually what I’m trying to do.

Thanks
/Max


#19

Sure. Once the one list is populated, you can copy it to another in a for loop. You can even do manipulations using formulas for the indexes (like reverse the order) as long as all the indexes are valid.


#20

Okay great. I think I can work with that. Thanks a bunch for the help dude. Strangest array objects I’ve ever dealt with :slight_smile: Btw, this is the thing I’m tinkering with. I’ve got 4 irrigation circuits which I (wifey) needs (wants) rather granular control over. I’m using 3 virtual dimmer devices for each circuit. The level slider values are for configuring duration for manual and automatic as well as start time in the AM. One of the switches (twice) allows one to add another water cycle at opposite hours: