How to use Difference Command with Arrays?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
merill001
Posts: 4
Joined: Mon Aug 01, 2022 8:56 pm

How to use Difference Command with Arrays?

Post by merill001 » Fri May 31, 2024 5:20 pm

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

stam
Posts: 3069
Joined: Sun Jun 04, 2006 9:39 pm

Re: How to use Difference Command with Arrays?

Post by stam » Fri May 31, 2024 5:59 pm

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: How to use Difference Command with Arrays?

Post by bn » Fri May 31, 2024 7:40 pm

merill001 wrote:
Fri May 31, 2024 5:20 pm
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"
That one tripped me also a while ago. The solution seems to be to declare the variable "destinationArray" as a local variable.
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
And as Stam mentioned it only looks at the KEYS not the values of the KEYS of the two arrays.


Kind regards
Bernd

merill001
Posts: 4
Joined: Mon Aug 01, 2022 8:56 pm

Re: How to use Difference Command with Arrays?

Post by merill001 » Fri May 31, 2024 8:27 pm

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.

merill001
Posts: 4
Joined: Mon Aug 01, 2022 8:56 pm

Re: How to use Difference Command with Arrays?

Post by merill001 » Fri May 31, 2024 8:33 pm

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

Post Reply