Creating an Undetermined Number of Variables

I’m wondering if it’s possible to create a certain number of variables depending on the number of items in a list. For instance given:

set myList to {"a". "b", "c", "d"}
set listResults to (choose from list myList with multiple selections allowed)
repeat with theResult in listResults
--make a new variable that points to an empty list for each selection with the only difference in variable names being either a number or the letters in the list (e.g., newList1, newList2, etc. OR newLista, newListb, etc.) so that it can be called from a loop later.
end repeat

Is this even possible? Any help would be greatly appreciated.

Yes, you can do this, but I’m not sure it makes possible what you hope. How will being able to do this help you to call the variables later in a list?

OK, let’s say I have a list of users {“Mike”, “Steve”, “Ray”, “Dan”, “Pete”} in a variable called userList. I’m trying to do this:


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

I need these variables so I can log which files the script is altering for which user. I can write a seperate routine for EVERY possible user and then just skip them if that user isn’t chosen, but this is not only much more time consuming, it’s much harder to scale. That’s why Im wondering if this can be done with a loop.

Oh, I see. What you need is a hash or associative list or dictionary. AS doesn’t have anything like that built-in, a serious lack. This was the problem I was thinking of above. You can dynamically create variables, but what does that get you? You have to hard code references to the variables into your script to use them efficiently in the rest of the code, so you gain no advantage. What you need, then, is a way to get back a list of values using a string as the key. For instance, you could have two lists, the first with a list of “variables” (names as strings), and the second whose corresponding items hold the lists of values.

There are public libraries available to do this. Look for ‘makeSDict’ on this page:

http://applemods.sourceforge.net/mods/Data/Types.php

Requires installing the vanilla AppleScript ‘Loader’ system.

Sure. You create variable numbers of variables with lists, or lists of lists, or lists of lists of lists, etc.

set myList to {“a”, “b”, “c”, “d”}

set userList to ¬
choose from list (myList) ¬
multiple selections allowed (true)

if (userList is not false) then

--	One of these basic structures should suit
--	your purpose (or coding style):
--
set structureType1 to {}
set structureType2 to {}

repeat with i from 1 to userList's length

	set oneUser to {}
	set oneUser's end to userList's item i --> "c"
	--
	set oneUser's end to 3.14159 -- default values
	set oneUser's end to "abc" --   for each user
	--
	set structureType1's end to oneUser


	set structureType2's end to userList's item i --> "c"
	--
	set oneUser to {}
	--
	set oneUser's end to 3.14159 -- default values
	set oneUser's end to "abc" --   for each user
	--
	set structureType2's end to oneUser

end repeat

end if

structureType1 → { { “c”, 3.14159, “abc” }, { “d”, 3.14159, “abc” } }

structureType2 → { “c”, { 3.14159, “abc” }, “d”, { 3.14159, “abc” } }

There are also the associative list hacks:

<http://applemods.sourceforge.net/files/Types.sit>
<http://www.esglabs.com/macscripts/map.sit.hqx>
<http://applemods.macscripter.net/getMod.php?script_ID=48>

These are script objects with methods that look something like this:

SetValue( "a", 3.14159 ) of AssociativeListObject
GetValue( "a" ) of AssociativeListObject --> 3.14159