Jump to content



Photo

_ArrayCompare


  • Please log in to reply
3 replies to this topic

#1 WideBoyDixon

WideBoyDixon

    Code Monkey

  • Active Members
  • PipPipPipPipPipPip
  • 381 posts

Posted 11 May 2009 - 01:00 PM

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, 20 May 2009 - 03:20 PM.






#2 WideBoyDixon

WideBoyDixon

    Code Monkey

  • Active Members
  • PipPipPipPipPipPip
  • 381 posts

Posted 13 May 2009 - 02:03 PM

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 ...

#3 Yashied

Yashied

    Happy in Moscow

  • MVPs
  • 2,512 posts

Posted 13 May 2009 - 07:52 PM

Very interesting.

One remark: I think it would be better to use ByRef for _ArrayCompare() function.

Func _ArrayCompare(ByRef $aLeft, ByRef $aRight)


#4 pillbug

pillbug

    Seeker

  • Active Members
  • 40 posts

Posted 06 July 2009 - 02:51 AM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users