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(…)


Lists & Arrays - documentation?
Clearing a list?
Creating and Accessing a WebCore String List (Text)
Date Countdown
Need your feedback on a new key-value piston input type in webCoRE
#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.


Variable loops in notification piston
Monitor Switch/Contact Sensors Status - need help
#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?


#11

Does this work anymore? I tried it and I get a new item with “*ALL” as the key. Same with *CLEAR.


#12

the webcore code has probably changed since then … i will take a look tomorrow and see if i can provide an updated version.


#13

Any news on this? Any ways of clearing a list on current version?


#14

@tyron thanks for the pull request, this will be in the next release!


#15

Thank @bangali. He created the solution. I just delivered the message :wink:


#16

Any luck with clearing lists?


#17

Is there a way to do this in the current version? Entering the index *ALL doesn’t appear to work. the variable remains blank


#18

How to cleaning a list?


#19

I tried this piston and got…


#20

I don’t see this code in the webCoRE code base (from ST)…

I don’t think this was accepted in as near as I can tell