[Solved] Script: Loop for + if/else

General discussions about Automagic and automation in general

Moderator: Martin

Post Reply
kamil_w
Posts: 50
Joined: 22 Oct 2013 19:01

[Solved] Script: Loop for + if/else

Post by kamil_w » 22 Mar 2019 07:08

Hi,

I have such code in action Script:


---------------------------

value = "pl_PL, en_GB, xx_XX";
list = newList(value);

for (i in list)
{

if (i == "pl_PL")
{
lang_pl = "Polish";
}

if (i == "en_GB")
{
lang_en = "English (British)";
}

else
{
lang_xx = "Unknown"
}

}

---------------------------


It doesn't work. Message:

value: {value}
list: {list}
i: {i}
lang_pl: {lang_pl}
lang_en: {lang_en}
lang_xx: {lang_xx}



Shows:

value: pl_PL, en_GB, xx_XX
list: [pl_PL, en_GB, xx_XX]
i: pl_PL, en_GB, xx_XX
lang_pl: null
lang_en: null
lang_xx: Unknown



What I expect to get is:

lang_pl: Polish
lang_en: English (British)
lang_xx: Unknown


What am I doing wrong?
Last edited by kamil_w on 22 Mar 2019 10:17, edited 1 time in total.

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: Script: Loop for + if/else

Post by anuraag » 22 Mar 2019 07:17

Use
list = newList("pl_PL", "en_GB" , "xx_XX");

kamil_w
Posts: 50
Joined: 22 Oct 2013 19:01

Re: Script: Loop for + if/else

Post by kamil_w » 22 Mar 2019 07:53

Thanks. It works that way, but I can't provide a string directly in function newList, as variable "value" is an output from previous action.

kamil_w
Posts: 50
Joined: 22 Oct 2013 19:01

Re: Script: Loop for + if/else

Post by kamil_w » 22 Mar 2019 09:43

I don't understand that:

Script:

Code: Select all

value = 'pl_PL, en_GB, xx_XX';
listvar = newList(value);

listdirect = newList("pl_PL", "en_GB", "xx_XX");

// elements for list taken form variable "value"
for (iv in listvar)
 {
   
       if (iv == "pl_PL")
         {
           lang_plv = "Polish";
         }

       if (iv == "en_GB")
         {
           lang_env = "English (British)";
         }

       else
         {
           lang_xxv = "Unknown"
         }
 
 }


// directly provided elements of list

for (id in listdirect)
 {
   
       if (id == "pl_PL")
         {
           lang_pld = "Polish";
         }

       if (id == "en_GB")
         {
           lang_end = "English (British)";
         }

       else
         {
           lang_xxd = "Unknown"
         }
 
 }



Check:

Code: Select all

value: {value}
listvar: {listvar}
listdirect: {listdirect}
iv: {iv}
id: {id}
lang_plv: {lang_plv}
lang_env: {lang_env}
lang_xxv: {lang_xxv}
lang_pld: {lang_pld}
lang_end: {lang_end}
lang_xxd: {lang_xxd}

Output:

Code: Select all


value: pl_PL, en_GB, xx_XX
listvar: [pl_PL, en_GB, xx_XX]
listdirect: [pl_PL, en_GB, xx_XX]
iv: pl_PL, en_GB, xx_XX
id: xx_XX
lang_plv: null
lang_env: null
lang_xxv: Unknown
lang_pld: Polish
lang_end: English (British)
lang_xxd: Unknown


So as both lists looks the same, than why the loop 'for' with listvar doesn't work as expected?

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: Script: Loop for + if/else

Post by anuraag » 22 Mar 2019 09:58

listvar contains one element "pl_PL, en_GB, xx_XX". While listdirect contain 3 elements pl_PL, en_GB, xx_XX. So its not same.

You can do this way
value = 'pl_PL, en_GB, xx_XX';
listvar = split(value, ",");

kamil_w
Posts: 50
Joined: 22 Oct 2013 19:01

Re: Script: Loop for + if/else

Post by kamil_w » 22 Mar 2019 10:16

Finally!
Great thanks!

Just once note: in variable "value" there must be no spaces after commas.

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

Re: [Solved] Script: Loop for + if/else

Post by Desmanto » 22 Mar 2019 10:50

I don't quite get what you actually want to do with the script. If value is fixed and you have the mapping for each value, wouldn't you get the same fixed value at every run?

If you need a multiple assignment using if in that method, it is better to use map instead, much shorter. But you still create lang_pl, which is a dynamic variable. I don't use dynamic variable often, but use dynamic map key instead. Here is the recration of your script, but using map method.

Code: Select all

value = "pl_PL, en_GB, xx_XX";
rvalue = replace(value, " ", ""); //remove spaces
slang = split(rvalue,",");

dblang = newMapFromValues(
"pl_PL", "Polish",
"en_GB", "English (British)");

langmap = newMap();
for(i in slang)
  langmap["lang_" + left(i,2)] = convertNull(dblang[i], "Unknown");
The result of the value mapped is stored at langmap keys. Use condition debug dialog to see the value
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.

kamil_w
Posts: 50
Joined: 22 Oct 2013 19:01

Re: [Solved] Script: Loop for + if/else

Post by kamil_w » 22 Mar 2019 11:00

Thank you Desmanto. I didn't know that.

However now I know why it didn't work. Let's leave that topic. I will describe my scenario in new topic. :)

Post Reply