Page 1 of 1

[Solved] Script: Loop for + if/else

Posted: 22 Mar 2019 07:08
by kamil_w
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?

Re: Script: Loop for + if/else

Posted: 22 Mar 2019 07:17
by anuraag
Use
list = newList("pl_PL", "en_GB" , "xx_XX");

Re: Script: Loop for + if/else

Posted: 22 Mar 2019 07:53
by kamil_w
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.

Re: Script: Loop for + if/else

Posted: 22 Mar 2019 09:43
by kamil_w
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?

Re: Script: Loop for + if/else

Posted: 22 Mar 2019 09:58
by anuraag
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, ",");

Re: Script: Loop for + if/else

Posted: 22 Mar 2019 10:16
by kamil_w
Finally!
Great thanks!

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

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

Posted: 22 Mar 2019 10:50
by Desmanto
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

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

Posted: 22 Mar 2019 11:00
by kamil_w
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. :)