Need help comparing sets of Arrays or Tables Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Need help comparing sets of Arrays or Tables

10 Jul 2019, 04:37

I have a Table (1) with "Name of Item" and "Quantity of Item". Then I have another table (2) with "Name of Item" and "Supplier Name".

I want to compare the "Name of Item" in both tables and when they match, store the "Name of Item" in a text file named with the matching "Supplier Name" for later orders and Delete the row containing the "Name of Item" and "Quantity of Item" from Table 1.

Example;
Table 1:

Cheese,2
Rice,3
Orange,4
Banana,2

Table 2:

Rice,Tonys Rice
Orange,Simons Vegetables

___________________________________________________________________________________________

Result:

Table 1:

Cheese,2
Banana,2

TonysRice.txt:
Rice,3

SimonsVegetables.txt:
Orange,4

Does anyone know a really fast way to do this? Both tables in my case are long.
Perhaps is it a good idea to store the columns of both tables in arrays and use a for loop with K and V to compare?
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Need help comparing sets of Arrays or Tables

10 Jul 2019, 06:24

Where are this tables? Are they stored as files?
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Need help comparing sets of Arrays or Tables

10 Jul 2019, 06:53

They are. I access their contents through FileRead.
Darayavahus
Posts: 36
Joined: 21 May 2016, 03:32

Re: Need help comparing sets of Arrays or Tables

10 Jul 2019, 08:27

Use loop file read to "ArrayName.Push(Value)" values into arrays.. Then: "Loop % ArrayName.MaxIndex()" to move through values in whole array, inside loop "Value := ArrayName[A_Index]" will provide you current value checked which can be compared using function below.


Months := Object()
Months := ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]
Months.Push("MAR")
Position := HasValue(Months,"MAR")

msgbox,,Test,Position: %Position%
Return

HasValue(haystack, needle) {
if(!isObject(haystack))
return false
if(haystack.Length()==0)
return false
for k,v in haystack
if(v==needle)
return A_Index
return false
}
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Need help comparing sets of Arrays or Tables  Topic is solved

10 Jul 2019, 08:28

Try this

Code: Select all

Table1=
(
Cheese,2
Rice,3
Orange,4
Banana,2
)
Table2=
(
Rice,Tonys Rice
Orange,Simons Vegetables
)
;~ or, if you want to popolate the arrays from file:
;~ FileRead, Table1, % "path for the table1 file"
;~ FileRead, Table2, % "path for the table2 file"

tab1Array := Object(StrSplit(Table1, ["`n",","])*)
tab2Array := Object(StrSplit(Table2, ["`n",","])*)
delArray := []
for k, v in tab1Array {
   if tab2Array.HasKey(k) {
      fname := tab2Array[k] ".txt"
      FileDelete, %fname%
      FileAppend, % k "," v, %fname%
      ;~ tab1Array.delete(k)    ; <---- Should be better to delete here the Item but it does not work
      delarray.Push(k)          ; so we save the matching Item in an array
   }
}
for k, v in delarray 
   tab1Array.delete(v) ; <---- now we delete the table1 matched item here. Here it works
for k, v in tab1Array 
   str .= k "`t" v "`n"
MsgBox % "New content in tab1Arr :`n" str
ExitApp
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Need help comparing sets of Arrays or Tables

11 Jul 2019, 06:41

Very elegant solution Odlanir, thanks. Even though I have been using arrays for quite sometime, and used AHK extensively, my understanding of Objects is very very limited and thus would not have found the solution without you.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Joey5, mikeyww, RandomBoy, wpulford and 332 guests