Variables in Repeats

Hey Gang,

Hope all’s well in the AS world.

Could someone please help me figure out how to re-define a variable from within a repeat loop.

If you take a look at the below script, I think you’ll see what I mean.


set cateringTotal to "12.34"
set coffeeTotal to "123.45"

set categoriesList to ¬
	{{name:"catering", value:cateringTotal}, {name:"coffee", value:coffeeTotal}}

repeat with thisCategoryValue in categoriesList
	
	--set this value to 	
	if (value of thisCategoryValue) contains "3.4" then set (value of thisCategoryValue) to "8.9"
	
	display dialog name of thisCategoryValue & " : " & (value of thisCategoryValue)
	
end repeat

display dialog "Coffee" & " : " & coffeeTotal  -- I NEED THIS TO REFLECT THE UPDATE MADE IN THE LOOP.

Basically, the final ‘display dialog’ should show the new value of 8.9.

I know I’m missing something simple but I’m stumped.

Cheers in advance.

Jase

Your script changed the value stored in the record, it didn’t changed the variable.
To achieve your goal you would code :

set cateringTotal to "12.34"
set coffeeTotal to "123.45"

set categoriesList to ¬
	{{name:"catering", value:cateringTotal}, {name:"coffee", value:coffeeTotal}}

repeat with thisCategoryValue in categoriesList
	
	--set this value to 	
	if (value of thisCategoryValue) contains "3.4" then
		set coffeeTotal to "8.9"
		set (value of thisCategoryValue) to "8.9" # or to coffeeTotal
	end if
	display dialog name of thisCategoryValue & " : " & (value of thisCategoryValue)
	
end repeat

display dialog "Coffee" & " : " & coffeeTotal -- I NEED THIS TO REFLECT THE UPDATE MADE IN THE LOOP.
item 2 of categoriesList

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) jeudi 19 mai 2016 12:14:23

Hey Yvan,

Thank you so much taking the time to respond.

I understand what you’ve said but I don’t think I’ve explained the whole issue sufficiently.

In my example, the ‘if’ statement will review each record in the list (there will be more records). As such, I can’t set the variable by specific name, it needs to be passed by the loop.

My first attempt had my list look like this:


set categoriesList to {cateringTotal, coffeeTotal, teaTotal, coldTotal}

But I was quite sure this was wrong as it passes only the values of those variables. I need to pass the actually name of the variable so I can set it and return it.

Does that make sense?

Jase

i may have missed the point but seems to work’’

set cateringTotal to "12.34"
set coffeeTotal to "123.45"

set categoriesList to {{name:"catering", value:cateringTotal}, {name:"coffee", value:coffeeTotal}}

repeat with thisCategoryValue in categoriesList
	
	--set this value to    
	if (value of thisCategoryValue) contains "3.4" then set (value of thisCategoryValue) to "8.9"
	
	--display dialog name of thisCategoryValue & " : " & (value of thisCategoryValue)
	set coffeeTotal to (value of thisCategoryValue) --if not 3.4 returns coffeeTotal
	
end repeat

display dialog "Coffee" & " : " & coffeeTotal -- I NEED THIS TO REFLECT THE UPDATE MADE IN THE LOOP.

Why not making a record so all objects can be accessed? From what I can see is that the name property is redundant and and since there is only one value you left you can assign it directly to the key:

set cateringTotal to "12.34"
set coffeeTotal to "123.45"

set categoriesList to {catering:cateringTotal, coffee:coffeeTotal}

-- this will never happen ;)
if coffee of categoriesList = "3.4" then set coffee of categoriesList to "8.9"

display dialog "Coffee" & " : " & coffee of categoriesList

English is not my main language so I’m not at ease to explain.
When you write the instruction :
set categoriesList to ¬
{{name:“catering”, value:cateringTotal}, {name:“coffee”, value:coffeeTotal}}
You store in the records, the value of each variable, you don’t store a pointer to them.
After the instruction, there is no link between the record and the variable.

The easy way to achieve your goal would be to code
set coffeeTotal to value of item of categoriesList whose name is “coffee”
Alas, as answered recently in an other thread this doesn’t work.

The correct syntax would be :

set cateringTotal to "12.34"
set coffeeTotal to "123.45"

set categoriesList to ¬
	{{name:"catering", value:cateringTotal}, {name:"coffee", value:coffeeTotal}}
set value of item 2 of categoriesList to "789"

repeat with aRecord in categoriesList
	if name of aRecord is "coffee" then
		set coffeeTotal to value of aRecord
		exit repeat
	end if
end repeat
coffeeTotal

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) vendredi 20 mai 2016 10:38:08

You can’t. However, if the variables are run-handler variables, globals, or properties, you can set references to them and set the ‘contents’ of those references:

set cateringTotal to "12.34"
set coffeeTotal to "123.45"

-- Tip: if you just type 'ref', it'll automatically expand to 'a reference to' on compilation.
set categoriesList to ¬
	{{name:"catering", value:a reference to cateringTotal}, {name:"coffee", value:a reference to coffeeTotal}}

repeat with thisCategoryValue in categoriesList
	
	if (value of thisCategoryValue) contains "3.4" then set (contents of value of thisCategoryValue) to "8.9"
	
	display dialog name of thisCategoryValue & " : " & (value of thisCategoryValue)
	
end repeat

display dialog "Coffee" & " : " & coffeeTotal

Another approach might be data sharing: set the variables to lists and set the contents of the lists:

set cateringTotal to {"12.34"}
set coffeeTotal to {"123.45"}

set categoriesList to ¬
	{{name:"catering", value:cateringTotal}, {name:"coffee", value:coffeeTotal}}

repeat with thisCategoryValue in categoriesList
	
	if (item 1 of value of thisCategoryValue) contains "3.4" then set (item 1 of value of thisCategoryValue) to "8.9"
	
	display dialog name of thisCategoryValue & " : " & (item 1 of value of thisCategoryValue)
	
end repeat

display dialog "Coffee" & " : " & (item 1 of coffeeTotal)