Assign strings in one list to variables in another list

This should be obvious and easy, but I’ve spent a lot of time trying to make it work.

I have two lists, a list of variables, like this:

set varList to {var0, var1, var2}

and a list of strings:

set stringList to {“string zero”, “string one”, “string two”}

I want to assign var0 to “string zero”, var1 to “string one”, and var2 to “string two”.

This brute-force code works:

set var0 to ""
set var1 to ""
set var2 to ""

set varList to {var0, var1, var2}
set stringList to {"string zero", "string one", "string two"}

set var0 to item 1 of stringList
set var1 to item 2 of stringList
set var3 to item 3 of stringList

But this doesn’t work, obviously because I don’t know how to handle lists:

set var0 to ""
set var1 to ""
set var2 to ""

set varList to {var0, var1, var2}
set stringList to {"string zero", "string one", "string two"}

repeat with i from 1 to the count of varList
	set item i of varList to item i of stringList
end repeat

What should I be doing that I’m not doing? (And if anyone knows a way to avoid those declarations of var0, etc., I’ll be glad to know what it is.)

Sorry for wasting bandwidth on something so simple.

OK, to answer my own question, I see that the correct form is:

set var0 to ""
set var1 to ""
set var2 to ""

set varList to {a reference to var0, a reference to var1, a reference to var2}
set stringList to {"string zero", "string one", "string two"}

repeat with i from 1 to the count of varList
	set contents of item i of varList to item i of stringList
end repeat

At least I think that’s right. I’ll be grateful for any further advice on this.

a simple solution to the script above would be:


set var0 to ""
set var1 to ""
set var2 to ""

set {var0, var1, var2} to {"string zero", "string one", "string two"}

Hello
I was puzzled by the original question :
I want to assign var0 to “string zero”, var1 to “string one”, and var2 to “string two”.
For me whose English is not the main language, I know how to assign a string value to a variable but not the opposite.

I understood that my reading was wrong when I received DJ Bazzie Wazzie’s answer.

As I’m lazy, at first time, I decided that initializing the three variables with empty strings was useless.

set stringList to {"string zero", "string one", "string two"}

set var0 to item 1 of stringList
set var1 to item 2 of stringList
set var2 to item 3 of stringList
set varList to {var0, var1, var2}

At second thought I drastically reduced the code to :

set stringList to {"string zero", "string one", "string two"}
copy stringList to {var0, var1, var2}
set varList to {var0, var1, var2}

Yvan KOENIG (VALLAURIS, France) mercredi 17 septembre 2014 11:06:21

Yvan,

This does exactly what I wanted. Thank you. My script generates the stringList when it runs, and then needs to set a variable to each item in that list. This does it in two lines. Very elegant!

Thanks again!