Jump to content

_ArrayCompare


Recommended Posts

I've had this kicking around for some time now. My original intention was to write a WinDiff style application in AutoIt but my enthusiasm for it was waned dramatically over the last couple of months. I've attached two files here. The first one contains the _ArrayCompare function which can be used to compare two one-dimensional arrays. My first reason for writing this was to use it after _FileReadToArray() to compare two similar files. However, I subsequently realised that I could use it to do other array comparisons from any of the AutoIt functions that return an array (WinList() for example). The second file contains some example code for how it could be used; together with a very basic WinDiff style dialog which has swathes of missing functionality :)

The basic premise is this. Call _ArrayCompare() with two one-dimensional arrays $aLeft and $aRight. It works best if they're at least similar. The return is an array of arrays which has two elements. The first element is the script for $aLeft and the second element is the script for $aRight. The scripts themselves are arrays with the same dimensions as the original arguments but with a zero or one in each entry to indicate whether the array entry at that point is unchanged (zero) or changed (one). By processing these scripts intelligently you can determine whether entries have been deleted/changed/inserted as appropriate.

Of course, I realise as I'm typing this that it all sounds rather complex. However, it should be straightforward so I'll try to explain a scenario:

Global $aLeft[9] = ["The", "big", "black", "cat", "sat", "on", "the", "mat", "again."]
Global $aRight[8] = ["The", "dog", "sat", "on", "the", "white", "cat", "ouch!"]
Glboal $aRet = _ArrayCompare($aLeft, $aRight)

From this code you should get:

$aRet[0] = [0, 1, 1, 1, 0, 0, 0, 1, 1]
$aRet[1] = [0, 1, 0, 0, 0, 1, 1, 1]

The zeroes show where the two arrays "line up" which is on ["The"] (element zero) and ["sat", "on", "the"] (elements 4-6 in $aLeft and 2-4 in $aRight).

I hope someone finds this useful in some way. My code is based on the diff algorithm from GNU but is much simplified for AutoIt implementation. The code can be found HERE.

As always, have fun.

WBD

Edited by WideBoyDixon
Link to comment
Share on other sites

Normally I wouldn't do this but since I spent a few days on this one I was hoping that someone would provide some feedback. I won't bump again I promise ...

Link to comment
Share on other sites

Link to comment
Share on other sites

  • 1 month later...

I've had this kicking around for some time now. My original intention was to write a WinDiff style application in AutoIt but my enthusiasm for it was waned dramatically over the last couple of months. I've attached two files here. The first one contains the _ArrayCompare function which can be used to compare two one-dimensional arrays. My first reason for writing this was to use it after _FileReadToArray() to compare two similar files. However, I subsequently realised that I could use it to do other array comparisons from any of the AutoIt functions that return an array (WinList() for example). The second file contains some example code for how it could be used; together with a very basic WinDiff style dialog which has swathes of missing functionality :)

The basic premise is this. Call _ArrayCompare() with two one-dimensional arrays $aLeft and $aRight. It works best if they're at least similar. The return is an array of arrays which has two elements. The first element is the script for $aLeft and the second element is the script for $aRight. The scripts themselves are arrays with the same dimensions as the original arguments but with a zero or one in each entry to indicate whether the array entry at that point is unchanged (zero) or changed (one). By processing these scripts intelligently you can determine whether entries have been deleted/changed/inserted as appropriate.

Of course, I realise as I'm typing this that it all sounds rather complex. However, it should be straightforward so I'll try to explain a scenario:

Global $aLeft[9] = ["The", "big", "black", "cat", "sat", "on", "the", "mat", "again."]
Global $aRight[8] = ["The", "dog", "sat", "on", "the", "white", "cat", "ouch!"]
Glboal $aRet = _ArrayCompare($aLeft, $aRight)

From this code you should get:

$aRet[0] = [0, 1, 1, 1, 0, 0, 0, 1, 1]
$aRet[1] = [0, 1, 0, 0, 0, 1, 1, 1]

The zeroes show where the two arrays "line up" which is on ["The"] (element zero) and ["sat", "on", "the"] (elements 4-6 in $aLeft and 2-4 in $aRight).

I hope someone finds this useful in some way. My code is based on the diff algorithm from GNU but is much simplified for AutoIt implementation. The code can be found HERE.

As always, have fun.

WBD

I've been looking for something like this for sorting some data. I've been using unix libraries, but this let's me sort the data in autoit.

Thanks so much for posting how you did this!

Link to comment
Share on other sites

  • 4 years later...

Hello WideBoyDixon,

I know this post is old but I need some help on this if you don't mind.

First of all, let me thank you for this, I think it is exactly what I was seeking for and I would like to ask you something :

You said "By processing these scripts intelligently you can determine whether entries have been deleted/changed/inserted as appropriate.". It seems I'm not as clever as I thought xD

So let me explain my problem. I try to develop a program in which it has to compare two texts and counting the mistakes made by the user (that means that I'm not really interested in knowing if the word has been changed, inserted or if it's missing).

The first one is an original one copied from a text file to a simple Edit control. The second text is entered by the user in a RichEdit (because I would like to color some words later).

So could you help me to count each mistake made by the user with your two "0 or 1" arrays? Like some kind of formula... I would be grateful for that :)

(The two arrays are already in my script and it's working well : "1" if there's a difference, "0" if there's none).

Edited by Liendil
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...