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?
Need help comparing sets of Arrays or Tables Topic is solved
Re: Need help comparing sets of Arrays or Tables
Where are this tables? Are they stored as files?
Re: Need help comparing sets of Arrays or Tables
They are. I access their contents through FileRead.
-
- Posts: 36
- Joined: 21 May 2016, 03:32
Re: Need help comparing sets of Arrays or Tables
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
}
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
}
Re: Need help comparing sets of Arrays or Tables Topic is solved
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
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Re: Need help comparing sets of Arrays or Tables
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.