Use variables in global variable names in strings


#1

1) Give a description of the problem

I tried asking this before, but got no takers. I’m trying it again. I’ve got four people with Life360 information stored. I’d like to call on the contents of global variables, but I want to place a variable in the global variable name so I can place it in a For-Next loop.

2) What is the expected behaviour?

The globals would be:

@LastAddressDerek
@LastAddressEric
@LastAddressTisha

I’d like to be able to use:

For Device = Derek, Eric, Tisha

Set variable LastAddress[inx] = @LastAddress{Device}

3) What is happening/not happening?

I cannot find the correct syntax for the variable statement.

**4) Post a Green Snapshot of the piston!

(Note: this is not the final piston. It’s one I created to try different ways to write the variable.


#2

Just thinking out loud here, in webCoRE we’re not able to set variable names programmatically afaik. It has to be explicitly defined beforehand wether it’s local or global but you could possibly set up a variable to be placeholder for the global names that you’re ‘assembling’.

I can see it in my head but I am not sure of the mechanics of it yet, or if it will even work :slight_smile: :gear::exploding_head:


#3

I was kinda thinking that as well, but then that would lead to something like @(variablename), which leads to the same problem. Even if I put them into an array to draw from, it would lead to the same problem. The globals and data already exist from another piston…I’m just trying to pull info from them in a particular way without listing every single one.

I’m pretty much given in to the realization that it’s probably not possible…it just would have helped cut down on some lines in my code. If you do come up with something, please let me know. :slight_smile:


#4

You can’t do what you are asking but perhaps you could define a variable using an expression:

string lastAddresses="{@LastAddressDerek},{@LastAddressEric},{@LastAddressTisha}"

As long as there are no commas in your addresses, then you can access each using:

for $index=0 to 2 step 1
   arrayItem($index, lastAddresses)
end for

Not sure what the contents of the address is if it is a name or an actual address.


#5

This was a project I came up with just for fun, and kept adding more and more stuff.

I have one Life360 piston that tells me when a family member has left a starred location (Home, Jack in the Box, Grandma’s House, etc.) It stores this info into globals (LastAddressEric, LastAddressEricSince, LastAddressEricDay). I have another piston that, when I ask Alexa where everybody is, she will pull the data for each person and, if they’re not home, will either tell me where they are (if it’s a starred location), or where they last were since what time. I was hoping to create a For-Next loop to pull all this information together, instead of line-by-line through a series of IFs. It works just fine as is, but just wanted to tighten up the code a little. It’s a bit of a monster.

Your idea sounds good. I’ve not done a lot of array coding like that, so I’ll have to try it out and see what I can do.

Piston One is for arrivals.

Piston Two keeps track of departures

Piston Three tells me the location of anyone not currently at home.