The Dictionary show:
difference targetArray with templateArray [into destinationArray]
How do you use the "[into destinationArray]" option?
The only example does not show how to use it.
If you add "into" and a variable name, you get a compilation error: (split:bad variable) near "destinationArray"
local tLeft, tRight
put "green" into tLeft["color"]
put "left" into tLeft["align"]
put "blue" into tRight["color"]
put "100" into tRight["width"]
difference tLeft with tRight into destinationArray
# RESULT
# the keys of tLeft = "align"
# tRight unchanged
How to use Difference Command with Arrays?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Re: How to use Difference Command with Arrays?
Difference removes the key:value pair from targetArray who's key:value pair are the same in the comparison ("template") array.
into tDestinationArray simply puts the pruned target array into a new array and leaves the original intact.
The question is what you are trying to do.
Use-cases for this are limited and there is a good chance something simpler may help.
So it would be helpful to know what you're trying to achieve so that others may offer concrete suggestions...
Stam
into tDestinationArray simply puts the pruned target array into a new array and leaves the original intact.
The question is what you are trying to do.
Use-cases for this are limited and there is a good chance something simpler may help.
So it would be helpful to know what you're trying to achieve so that others may offer concrete suggestions...
Stam
Re: How to use Difference Command with Arrays?
That one tripped me also a while ago. The solution seems to be to declare the variable "destinationArray" as a local variable.merill001 wrote: ↑Fri May 31, 2024 5:20 pmThe Dictionary show:
difference targetArray with templateArray [into destinationArray]
How do you use the "[into destinationArray]" option?
The only example does not show how to use it.
If you add "into" and a variable name, you get a compilation error: (split:bad variable) near "destinationArray"
As soon as you declare it the compilation error is gone. You only have to declare the destinationArray but it is a good but tedious habit to declare all variables and turn "Strict Compilation Mode" on the the Preferences Stack for Script Editor. Especially when writing longer scripts. A typo can be very in a variable name can be hard to track down.
Code: Select all
on mouseUp
local tLeft, tRight, tDestinationArray
put "green" into tLeft["color"]
put "left" into tLeft["align"]
put "blue" into tRight["color"]
put "100" into tRight["width"]
difference tLeft with tRight into tDestinationArray
put "the key/keys of differenceArray are: " & the keys of tDestinationArray
end mouseUp
Kind regards
Bernd
Re: How to use Difference Command with Arrays?
My use case is that I'm writing as a proof of concept, a JSON file database in pure LiveCode.
It loads a JSON file and converts it to a LiveCode Array for processing the typical CRUD operations. All operation would be on LiveCode arrays with the results written back to JSON data file so basically an in memory database with local disk storage.
The keys in my case are indexes of the data so I'm not concerned with the values for this operation.
For Create function, I was looking for a fast way to compare keys of a multidimensional array of the loaded JSON data to the new record that is being created to avoid duplicated entries or overwrites.
It loads a JSON file and converts it to a LiveCode Array for processing the typical CRUD operations. All operation would be on LiveCode arrays with the results written back to JSON data file so basically an in memory database with local disk storage.
The keys in my case are indexes of the data so I'm not concerned with the values for this operation.
For Create function, I was looking for a fast way to compare keys of a multidimensional array of the loaded JSON data to the new record that is being created to avoid duplicated entries or overwrites.
Re: How to use Difference Command with Arrays?
Bernd,
Thanks for your tip about it requiring a declared local variable! That solved my problem.
It would be real helpful if the dictionary included this requirement.
Best regards,
Lloyd
Thanks for your tip about it requiring a declared local variable! That solved my problem.
It would be real helpful if the dictionary included this requirement.
Best regards,
Lloyd