Stuffing a list: *ALL


#1

@ady624: while i was in there doing the *CLEAR

https://community.webcore.co/t/clearing-a-list/1345/11?u=bangali

thought i might as well add a stuff method for lists. so, added a stuff method for use like this

set variable lightNames[*ALL] = “0:light 1,1:light 2,2:light 3”

which gives:

lightNames = {“0”:“light 1”, “1”:“light 2”, “2”:“light 3”}

seems to be working as desired. not sure if there might an unwanted side effect from replacing the entire variable value instead of just a single indexed value in the list?

thanks!

/******************************************************************************/
/*** stuff returns a map used to initialize a list variable. called from	***/
/*** setvariable when set variable list[*ALL] = "idx:value,idx:value"		***/
/******************************************************************************/
private stuff(rtData, variable, value) {
	if (!value)
    	return [t: "error", v: "Invalid value. Expecting value in form 'idx:value,idx:value'"];
	def list = value.split(',').toList()
	if (list.size() <= 0)
		return [t: "error", v: "Invalid value. To clear the list use set variable list[*CLEAR] = ''"];
	def mappedValue = [:]
	list.each	{
        def splitIt = it.indexOf(':')
        def listIDX = it.substring(0, splitIt)
        def listValue = it.substring(splitIt+1, it.size())
		if (listIDX.size() < 1)
			mappedValue << [t: "error", v: "Invalid value. Each list item must be in form 'idx:value'"];
        else
			mappedValue.put(listIDX, cast(rtData, listValue, variable.t.replace('[]', '')));
	}
	return mappedValue
}

EDIT: updated to use cast(…)


Creating and Accessing a WebCore String List (Text)
Date Countdown
Need your feedback on a new key-value piston input type in webCoRE
Clearing a list?
Lists & Arrays - documentation?
#2

Uh… what is this?


#3

heres an example to show the basic functionality of this

remember to update the testString accordingly after selecting the devices, order does not matter.


Monitor Switch/Contact Sensors Status - need help
Variable loops in notification piston
#4

Nice…

I can see this being useful!


#5

thanks. just need one more thing a $firstRun flag so, the lists can get initialized only if its first run for the piston. rest of the runs for the piston the list values should be just directly accessed via IDX.


#6

Couldn’t you just create a variable called firstRun and set it to false at the end of the piston? Then for every other run it will be false?


#7

that works unless you edit the piston. so, say i did that in the example piston above. it would work till i needed to edit the piston to add some more devices. once i add the devices and updated testString the strings[] list will not get updated on the next run because firstRun is already false.

also, might have to use an is true check instead of a is false check because null might be evaluated as false in groovy … havent checked this one.


#8

Good point… So it is the first run for the piston, since it was last edited…?

And yeah - I would probably do it as firstRunCompleted and set it to true.


#9

yes, since it was last saved.

or if the empty variables get initialized when a piston is saved that would work too.


#10

and while i am on reminders … @ady624 or @ipaterson?