Creating unique string variables from a list

Post your questions and help other users.

Moderator: Martin

Post Reply
skiptannen
Posts: 82
Joined: 13 Jan 2014 21:39

Creating unique string variables from a list

Post by skiptannen » 05 May 2020 15:49

I can't find a solution in the forum and I can't figure out how to write the script - I simply want to create unique variables for each string value in a list. For example, for the following list:

Code: Select all

list=newList("one","two","three");
I would like to end up with the following variables:

var_string_1 = "one"
var_string_2 = "two"
var_string_3 = "three"

I'm sure this is simple with the correct script syntax but it's escaping me...

Thanks in advance.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Creating unique string variables from a list

Post by Desmanto » 05 May 2020 17:39

use setValue() to create new variable name from list/map.

There are several ways. Below are 2 examples

Code: Select all

list = newList("one","two","three");
for(i in list)
  setValue(i, i); //one = "one"

for(i in [0 to length(list)-1])
  setValue("var_string_{i+1}" , list[i]); //var_string_1 = "one"
Assuming your list contains unique element and conform to variabel naming pattern, then you can use the first method to create variable name directly from the content. But it won't work if you have duplicates or the string is something not variable-name friendly.

Second method is to use the index and add 1, then concat it to prefix var_string_ This create uniform numbered variable from the list. It works with duplicate and the result of the variable also has the same order as the list.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

skiptannen
Posts: 82
Joined: 13 Jan 2014 21:39

Re: Creating unique string variables from a list

Post by skiptannen » 05 May 2020 20:04

That's perfect - thanks so much Desmanto. I really appreciate the example and the explanations. I can now get the flow working and I have a much better understanding.

Cheers!

Post Reply