Matrix / array / object handling

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Matrix / array / object handling

Post by Albireo » 20 May 2022, 18:59

I have a CSV file to handle.
There are no column names in the file and the column separator has been changed from ";" to ","
At the beginning of the script below, a comma-separated csv file with column names in UTF-8 is created.
Because fields can contain "," (comma), the best is to use to use Loop parse, A_LoopReadLine, CSV, when the file are handling.

Code: Select all

SetBatchLines -1  ; Kör programmet så fort som möjligt
#SingleInstance Force	; Endast "ett" program kan köras samtiodigt
#NoEnv	; Undviker kontroll av tomma variabler för att se om dessa är miljövariabler, Ökar prestandan och kompatibilitet med framtida AutoHotkey ver. (rekommenderas till alla nya script)

; The main file
colName := "
( LTrim Join QC		; Q = Quotes, C = Comments
 	""levArtNr""		; N-2401S
	,""benämning""		; S-KROK
	,""exTxt1""			; LÄNGD 8,5 CM
	,""exTxt2""			; SVART
	,""inPris""			; 3.36				
	,""antal""			; 24					
	,""bestKv""			; 24					
	,""userNr""			; 114					
	,""user""			; Elvira Norén		
	,""okänd1""			;						(okänd funktion)
	,""lev""				; FLOWERPOWER		
	,""levName""		; AB Flower Power Import
	,""levAdr1""		; Vapenvägen 8
	,""levAdr2""		;
	,""Ort""				; 645 33  NOWHERE
	,""okänd2""			;						(okänd funktion)
	,""to""				; Red Riding Hood
	,""toMail""			; [email protected]
	,""tfn""				; 0583 14570
	,""fax""				; 0583 10018
	,""date""			; 2022-05-16
	,""clock""			; 10:19:44
	,""typ""				; ORD
	,""varugrp""		; 3350
	,""godsMärk""		; Heminredning
	,""avrop""			;
	,""multi""			;
	,""fraktGräns""	; 15000
)"

; - - - - - - - - - - - - - - - - - - - - 
; Create and read a test-CSV
regInfo = 
( LTrim
	"J-213441","ÄNGEL","18 X 27 CM","SORTERAT","113.52",6,6,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:12:51","ORD",3350,"Heminredning",,,15000
	"J-123484","ÄNGEL","HÖJD 45 CM","SORTERAT","122.32",4,4,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:12:47","ORD",3350,"Heminredning",,,15000
	
	"N-21343W","KEDJA MED KROK","LÄNGD 55 CM","VIT","6.16",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:55","ORD",3350,"Heminredning",,,15000
	"N-4761350S","AMPELKEDJA","LÄNGD 80 CM","SVART","25.08",18,18,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:32","ORD",3350,"Heminredning",,,15000
	"0012400","S-KROK","LÄNGD 5,5 CM","SVART","3.36",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:48","ORD",3350,"Heminredning",,,15000
	,,,,,,,,,,,,,,,,,,,,,,,,,,,
)
sourceInfo := colName "`n" regInfo
; MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % colName
; MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % regInfo
; MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % sourceInfo


tmpFile1 := A_WorkingDir "\tmp_testfile.csv"
If !FileExist(tmpFile1)	; Create a new sourcefile if not exist
{	FileAppend %sourceInfo%, %tmpFile1%, UTF-8
	If ErrorLevel
	{	SplitPath tmpFile1, OutFileName, OutDir
		MsgBox 16, Row %A_LineNumber% -> %A_ScriptName%,
		( LTrim
			Couldn't create the file!`n
			File name .: %OutFileName%
			Path .: %OutDir%
			Is the file opened by another program?`n
			This program will be stopped!
		)
		ExitApp
	}
}
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/*
FileRead fileInfo, *t *P65001 %tmpFile1%
If ErrorLevel
{	SplitPath tmpFile1, OutFileName, OutDir
	MsgBox 16, Row %A_LineNumber% -> %A_ScriptName%,
	( LTrim
		Unknown error! `t(ErrorLevel .: %ErrorLevel%)`n
		Couldn't read or found the file.
		File name . . .:`t%OutFileName%
		Path .: %OutDir%`n
		This program will be stopped!
	)
	ExitApp
}
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % fileInfo
*/

; Show each fields
FileEncoding UTF-8
Loop read, %tmpFile1%
{	lineNumber := A_Index
	Loop parse, A_LoopReadLine, CSV
	{	MsgBox 4, Row %A_LineNumber% -> %A_ScriptName%, Field %lineNumber%-%A_Index% is:`n%A_LoopField%`n`nContinue?
		IfMsgBox, No
			Return
       }
}
My wish is to create an array / object where I have the opportunity to retrieve field information from the matrix above.

Assume that the result array is called infoCol (or something else)
To reach a specific information I want to write infoCol(column1, header)
eg. an instruction like infoCol("J-213441", "exTxt1") gives the result 18 X 27 CM
or
colName := "exTxt2"
ProdName := "0012400"
infoCol(prodName, colName)
gives the result SVART

information about the source
a)
Assume that the first column will not contain any duplicates.
But If duplicates exist only the column6 "antal" is added to the first product.
(If it's hard to solve - ignore my wish (it happens once or twice a year - I can handle it another way))

b)
the regInfo in the testfile have both empty rows and if the first column is empty the "row" is ignored.

c)
It is easy for the 0s to disappear at the beginning of 0012400 (the text is converted to a number)

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Matrix / array / object handling

Post by mikeyww » 20 May 2022, 19:37

The skeleton:

Code: Select all

csv = %A_ScriptDir%\test.csv
Gosub, Init
MsgBox, 64, Result, % infoCol("J-213441", "exTxt1")
colName  := "exTxt2"
prodName := "0012400"
MsgBox, 64, Result, % infoCol(prodName, colName)
Return

infoCol(prodName, colName) {
 Return prop[prodName][colName]
}

Init:
colName =
(
levArtNr
benämning
exTxt1
exTxt2
inPris
antal
bestKv
userNr
user
okänd1
lev
levName
levAdr1
levAdr2
Ort
okänd2
to
toMail
tfn
fax
date
clock
typ
varugrp
godsMärk
avrop
multi
fraktGräns
)
header := StrSplit(colname, "`n")
Global prop := {}
If !FileExist(csv) {
 MsgBox, 48, Error, File not found. Aborting`n`n%csv%
 ExitApp
}
Loop, Read, %csv%
 Loop, Parse, A_LoopReadLine, CSV
  (A_Index = 1) ? levArtNr := A_LoopField : prop[levArtNr, header[A_Index]] := A_LoopField
Return
Attachments
test.csv
CSV file
(1.43 KiB) Downloaded 19 times

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Matrix / array / object handling

Post by Albireo » 21 May 2022, 03:42

Thank you! (I'm impressed)

I try to understand... (can you explain a little?)
  • Check if sourcefile exist - Yes!
  • Handle empty rows - Yes!
To handle UTF-8 files I added

Code: Select all

...
FileEncoding UTF-8
Loop, Read, %csv%
But there is a problem
Article 0012400 is not the same as 012400

a new source information (with 0012400 and 012400 and a duplicate N-4761350S)

Code: Select all

"J-213441","ÄNGEL","18 X 27 CM","SORTERAT","113.52",6,6,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:12:51","ORD",3350,"Heminredning",,,15000
"J-123484","ÄNGEL","HÖJD 45 CM","SORTERAT","122.32",4,4,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:12:47","ORD",3350,"Heminredning",,,15000
"012400","KROK","DIAMETER 5,5 CM","RÖD","3.36",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:48","ORD",3350,"Heminredning",,,15000
"N-4761350S","AMPELKEDJA","LÄNGD 80 CM","SVART","25.08",18,18,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:32","ORD",3350,"Heminredning",,,15000

"N-21343W","KEDJA MED KROK","LÄNGD 55 CM","VIT","6.16",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:55","ORD",3350,"Heminredning",,,15000
"N-4761350S","AMPELKEDJA","LÄNGD 80 CM","SVART","25.08",18,18,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:32","ORD",3350,"Heminredning",,,15000
"0012400","S-KROK","LÄNGD 5,5 CM","SVART","3.36",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:48","ORD",3350,"Heminredning",,,15000
,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Matrix / array / object handling

Post by mikeyww » 21 May 2022, 05:24

So you can add some code to do what you want to do with the zeroes. You can strip leading zeroes if you wish, and just use what is left. You would then determine how you want to handle duplicates, whether it is overriding, comparing, or ignoring. That can be done in the CSV loop-- checking to see whether the array key already exists.

Code: Select all

str       = 0004120
stripped := RegExReplace(str, "^0+")
MsgBox, 64, Result, Original = %str%`n`nNew       = %stripped%

Code: Select all

str       = 0004120
stripped := str + 0
MsgBox, 64, Result, Original = %str%`n`nNew       = %stripped%
Explained: HasKey

Code: Select all

prop := {a: [1, 2], b: [3, 4]}
MsgBox, 64, Result, % prop.HasKey("b")

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Matrix / array / object handling

Post by Albireo » 22 May 2022, 03:04

No!

I want to have the zeros left. In your example 004120 is the same as 04120

my whish is

Code: Select all

colName  := "exTxt2"
prodName := "0012400"
MsgBox, 64, Result, % infoCol(prodName, colName)
shall give the result BLACK

Code: Select all

colName  := "exTxt2"
prodName := "012400"
MsgBox, 64, Result, % infoCol(prodName, colName)
shall give the result RED
(Now the result will be BLACK in both cases)

How duplicates should be handled can be a little different (I intend to use this structure for several different solutions.)
In this case I want to sum the numbers in the column antal
from the sample file.
N-4761350S is on two rows
Everything is the same on this rows but not the column antal (also maybe time and date are different - but is irrelevant in this case)
(I can try to explain more later)

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Matrix / array / object handling

Post by BoBo » 22 May 2022, 04:14

Are 004120 vs 04120 validated as strings or numbers?

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Matrix / array / object handling

Post by mikeyww » 22 May 2022, 05:37

Replacement lines:

Code: Select all

Loop, Read, %csv%
 Loop, Parse, A_LoopReadLine, CSV
  (A_Index = 1) ? levArtNr := A_LoopField : prop["p" levArtNr, header[A_Index]] := A_LoopField
Return

infoCol(prodName, colName) {
 Return prop["p" prodName][colName]
}

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Matrix / array / object handling

Post by Albireo » 22 May 2022, 15:43

Thank you!

Changed a bit in the test file (CSV) which now looks like this .:

Code: Select all

"J-213441","ÄNGEL","18 X 27 CM","SORTERAT","113.52",6,6,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:12:51","ORD",3350,"Heminredning",,,15000
"J-123484","ÄNGEL","HÖJD 45 CM","SORTERAT","122.32",4,4,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:12:47","ORD",3350,"Heminredning",,,15000
"012400","KROK","DIAMETER 5,5 CM","RÖD","3.36",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:48","ORD",3350,"Heminredning",,,15000
"N-4761350S","AMPELKEDJA","LÄNGD 80 CM","SVART","25.08",10,10,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:32","ORD",3350,"Heminredning",,,15000

"N-21343W","KEDJA MED KROK","LÄNGD 55 CM","VIT","6.16",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:55","ORD",3350,"Heminredning",,,15000
"N-4761350S","AMPELKEDJA","LÄNGD 80 CM","SVART","25.08",30,10,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:32","ORD",3350,"Heminredning",,,15000
"0012400","S-KROK","LÄNGD 5,5 CM","SVART","3.36",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:48","ORD",3350,"Heminredning",,,15000
,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,
In the main file, I changed p to ? (there is a probability that an article number can be P0456 but never ?0456 - do not think it adds any other problem)
I also added encode UTF-8

Now the file looks like this .:

Code: Select all

#NoEnv	; Undviker kontroll av tomma variabler för att se om dessa är miljövariabler, Ökar prestandan och kompatibilitet med framtida AutoHotkey ver. (rekommenderas till alla nya script)
; #Warn
#SingleInstance Force	; Endast "ett" program kan köras samtiodigt
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetBatchLines -1  ; Kör programmet så fort som möjligt

csv = %A_ScriptDir%\test.csv
Gosub, Init

; MsgBox, 64, Result, % infoCol("J-213441", "exTxt1")

colName  := "exTxt2"
prodName := "0012400"
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % infoCol(prodName, colName)

colName  := "exTxt2"
prodName := "012400"
MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % infoCol(prodName, colName)

MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % infoCol("N-4761350S", "antal")

ExitApp

Init:
	colName =
	( LTrim
		levArtNr
		benämning
		exTxt1
		exTxt2
		inPris
		antal
		bestKv
		userNr
		user
		okänd1
		lev
		levName
		levAdr1
		levAdr2
		Ort
		okänd2
		to
		toMail
		tfn
		fax
		date
		clock
		typ
		varugrp
		godsMärk
		avrop
		multi
		fraktGräns
	)
	header := StrSplit(colname, "`n")
	
	Global prop := {}
	If !FileExist(csv)
	{	MsgBox, 48, Error, File not found. Aborting`n`n%csv%
		ExitApp
	}

	FileEncoding UTF-8
	Loop Read, %csv%
	{	Loop Parse, A_LoopReadLine, CSV
		{	(A_Index = 1) ? levArtNr := A_LoopField : prop["?" levArtNr, header[A_Index]] := A_LoopField
		}
	}
Return


infoCol(prodName, colName)
{	Return prop["?" prodName][colName]
}
I have not tested the speed of the program. - yet ;)
(The original csv file may contain 45,000 lines. - But I do not think it will be a problem)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

In this case, duplicates are not a problem.
But would it be difficult to handle duplicates?
(I have another case - similar to this, but with duplicates - as I want to handle)

In the CSV file above, I have added a duplicate N-4761350S
the first N-4761350S has 10 as antal and the second N-4761350S has 30 as antal
My wish is that N-4761350S get the antal = 40 (30+10)
eg. MsgBox % infoCol("N-4761350S", "antal") give the result 40

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Matrix / array / object handling

Post by mikeyww » 22 May 2022, 16:28

I think you can code it. Add the new value to the existing value, or 0 if there is no existing value.

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Matrix / array / object handling

Post by Albireo » 25 May 2022, 08:10

I have done some tests (to try to understand my wish myself :) )
Handles duplicates - but does not create the desired array
Realize that I should / must change my desire to reach my goal.

Assume the following source data (below) for testing.
  • Empty row / empty field
  • Duplicate
  • Article number with one or two zero in the beginning
  • CSV-file (separated by commas)

Code: Select all

"levArtNr","benämning","exTxt1","exTxt2","inPris","antal","bestKv","userNr","user","okänd1","lev","levName","levAdr1","levAdr2","Ort","okänd2","to","toMail","tfn","fax","date","clock","typ","varugrp","godsMärk","avrop","multi","fraktGräns"
"J-213441","ÄNGEL","18 X 27 CM","SORTERAT","113.52",6,6,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:12:51","ORD",3350,"Heminredning",,,15000
"J-123484","ÄNGEL","HÖJD 45 CM","SORTERAT","122.32",4,4,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:12:47","ORD",3350,"Heminredning",,,15000
"012400","KROK","DIAMETER 5,5 CM","RÖD","3.36",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:48","ORD",3350,"Heminredning",,,15000
"N-4761350S","AMPELKEDJA","LÄNGD 80 CM","SVART","25.08",10,10,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:32","ORD",3350,"Heminredning",,,15000

"N-21343W","KEDJA MED KROK","LÄNGD 55 CM","VIT","6.16",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:55","ORD",3350,"Heminredning",,,15000
"N-4761350S","AMPELKEDJA","LÄNGD 80 CM","SVART","25.08",30,10,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-17","11:14:07","ORD",3350,"Heminredning",,,15000
"0012400","S-KROK","LÄNGD 5,5 CM","SVART","3.36",24,24,114,"Elvira Norén","XX","FLOWERPOWER","AB Flower Power Import","Vapenvägen 8",,"645 33  NOWHERE",,"Red Riding Hood","[email protected]","0543 14000","0543 14001","2022-05-16","10:19:48","ORD",3350,"Heminredning",,,15000
,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,
My desire (now) is to create an object with the following structure.
prodData[Column1, The heading] eg. prodData[0012400, exTxt1] (or maybe prodData[0012400, "exTxt1"])
(Which would produce the result .: LÄNGD 5,5 CM)
This program creates an object - but with an inverted structure
prodData[heading, column1] (instead of prodData[column1, heading] eg. prodData[000030-333-00, art_main_name]).

- . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . - . _ . -

The advantage of creating an array of each article number is, that it becomes very easy to check if the article number already exists.
eg. if isObject(prodData) - then managed depending on the wishes and structure of the CSV file. (in this case the field "number" must be summed)
Although in some cases there will be many arrays (40-50,000), I do not think this will be a problem).

The desire is to write a AHK-function() like this .:
(inputValue is the CSV-source)

Code: Select all

...
prodData := dataCreate(inputValue)
MsgBox % prodData[000030-333-00, art_main_name]
ExitApp

dataCreate(inputValue)
{	...
	Return % aResult
}
The following test CSV-files should work, but duplicates are handled individually
Test CSV2
Test CSV2


My largest trouble (right now) is .:
  • What should the structure look like?
  • Which arrays to create? (key?, value?)
  • How to put these together?

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Matrix / array / object handling

Post by mikeyww » 25 May 2022, 08:25

My script matches your description.

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Matrix / array / object handling

Post by Albireo » 25 May 2022, 09:15

I may have misunderstood something? (I do not understand the structure)
yes! the answer is correct. eg. infoCol("N-4761350S", "antal") give 30 (before duplicates are handled)

But, I don't know how to search N-4761350S (as Object)
(If all article numbers are to be compared with each other, there will be very many comparisons - 6 result rows give 19 compares)

In your solution

Code: Select all

for k, v in prop
s .= "k - " k "`tv - " v "`n"
MsgBox ,,, % s
Give the following result .:
k - ? v -
k - ?0012400 v -
k - ?012400 v -
k - ?J-123484 v -
k - ?J-213441 v -
k - ?N-21343W v -
k - ?N-4761350S v -

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Matrix / array / object handling

Post by mikeyww » 25 May 2022, 09:26

Your stated goal is to search an object.

Code: Select all

levArtNr = N-4761350S
For heading, cell in prop["p" levArtNr]
 MsgBox, 64, %heading%, %cell%

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Matrix / array / object handling

Post by Albireo » 30 May 2022, 15:03

Thank you!

It became much easier to handle after you showed me, how to get values from the object.
(eg. prop[artNr] works prop.artNr doesn't work)

I managed to handle duplicates in the following way .:
Handling of duplicates


Now I try to manage csv-files with / without headings.
But I can not get FileReadLine to work.
(%A_ScriptDir%\test1a.csv exist but gives ErrorLevel = 1

Code: Select all

csvFile = %A_ScriptDir%\test1a.csv  ; a CSV file with headings
; Check if the CSV-file exist
If !FileExist(csvFile)
{	MsgBox 48, Row %A_LineNumber% -> %A_ScriptName%, % "CSV-file not found! `n`n Aborting`n`n" csvFile
	ExitApp
}
	
FileReadLine outLine, csvFile, 1
if ErrorLevel
	MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % "ErrorLevel .: " ErrorLevel "`n`n" csvFile "`n`n- " outLine

MsgBox ,, Row %A_LineNumber% -> %A_ScriptName%, % "- " csvFile "`n- " outLine


After that empty rows must be handled - but this is a later problem.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Matrix / array / object handling

Post by mikeyww » 30 May 2022, 16:29

AHK commands would typically flank a parameter in % if using a variable, except where noted otherwise. Do it for your source file in FileReadLine.

Post Reply

Return to “Ask for Help (v1)”