Arrays and Function() - Is this unable?

Get help with using AutoHotkey and its commands and hotkeys
Albireo
Posts: 399
Joined: 16 Oct 2013, 13:53

Arrays and Function() - Is this unable?

Post by Albireo » 09 Feb 2018, 17:42

Have made a test file with two tables to be converted to arrays.
Would use the same function, but I have not succeed.

Must a separate function be created, for each Array to be handled?
(Or have I missed something significantly)

Code: Select all

#SingleInstance	force
#NoEnv

InputData1 = 
(LTrim Join
	"ArtNo";"Head1";"Head2";"Count";"Supplier"`n
	"00053";"Info1";"Info2";"35";"Supp"`n
	"001589-045-00";"Info1a";"Info2a";"128";"Supp1"`n
)

InputData2 = 
(LTrim Join
	"art_id";"Art_Head1";"Art_Head2";"CountNo";"art_supp"`n
	"GT101264-1";"Info1b";"Info2b";"0";"Supp2"`n
	"939431-1";"Info1c";"Info2c";"17";"Supp"`n
)


Array1 := DataRead(InputData1, A_LineNumber)
Array2 := DataRead(InputData1, A_LineNumber)
ShowMatrix(Array1, A_LineNumber)
ShowMatrix(Array2, A_LineNumber)
ExitApp

DataRead(InputStr, CallFromNo)
{	TableContent := {}
	; Line = 0
	Loop Parse, InputStr, `n	; Loop Parse, InputData, `n, `"
	{	If ( StrLen(A_LoopField) < 1 )	; Ignore completely blank lines
			Continue
		
		Line += 1
		If ( Line == 1 )
		{	Columns := StrSplit(A_LoopField, ";", """")
			For ID, Column in Columns	; ex. Columns = {  1:"ArtNo"   2:"Barcode1"   3:"Barcode2"   4:"Barcode3"   5:"Barcode4"   6:"SuppArtNo"  }
			{	TableContent[Column] := {}	; Create a new empty Array for each column - like { ArtNo:"" ,BarCode1:"", BarCode2:"" ...}
			}
			Continue
		}
		else
		{	Row := StrSplit(A_LoopField, ";", """")
			For ID, Column in Columns
			{	TableContent[Column, Row[ID] . ""] := Line
				; MsgBox 64, Row.: %A_LineNumber% -> %A_ScriptName%, % "ID .: " ID "`nLine .: " Line "`nRow[ID] " Row[ID] "`nColumn .: " Column "`n- " TableContent["ArtNo", 00053 . ""] "`n- " TableContent[Column, Row[ID] . ""]
			}
		}
	}
	Return TableContent
}


; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; - -   B e l o w   - -   O n l y  -  h e l p  -  f u n c t i o n s   - - - -
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ShowArray(ArrayName, JumpFromLine)
{	; Version .: 09 feb 2018
	; Handles both calls of Object and Variables of an object
	; - e.g. Call with a variable .: ShowArray("Color", A_LineNumber)
	; - e.g. Call with an Object.: ShowArray(Color, A_LineNumber)
	; The program continues even if "ArrayName" is NOT an array.
	; Displays both "KEY" and "Value" - e.g. [ArtNo: 1]
	; At the bottom of MsgBox, is a summary displayed.
	; All Keys and result is copied to the Clipboard
	
	FunctionName = ShowArray	; To know the name of this function
	MaxRows = 15	; Maximum number of records as displayed.
	
	; Check and handle if the "ArrayName" is an Object or a String
	If StrLen(ArrayName)
	{	ObjectName = %ArrayName%
		ObjName = %ArrayName%
	}
	else
	{	ObjectName = Unknown
		ObjectText = (The "ArrayName" is not a variable)
		ObjName = ArrayName
	}
	
	/*
	; Does not work as desired
	; The Array exist but have no keys or values
	; Handle an Array that is not available in the same way
	If ArrayName.Length() < 1
	{	MsgBox 64, Row.: %A_LineNumber% -> %A_ScriptName%,
		( LTrim Join
		%	"Unsure if the Array is empty or exist! `n`n
			Function name .: " FunctionName "`n
			Call from line .: " JumpFromLine "`n`n
			This program continues!"
		)
		Return
	}
	*/

	If !IsObject( %ObjName% )	; ObjName/(ArrayName) - is not an Array
	{	MsgBox 64, Row.: %A_LineNumber% -> %A_ScriptName%,
		(LTrim Join
		%	"Array .: ( " ObjName " ) Does not exist! `n`n
			Function name .: " FunctionName "`n
			Call from line .: " JumpFromLine "`n`n
			This program continues!"
		)
		Return	
	}
	
	; Show the Array!
	ListHead := "Function .: " FunctionName "`n- - - - - - - - - - - - - - - - - - - -`n`n"
	For i, Value in %ObjName%	; For "Key", "Value" i SimpleArray - Get the key!
	{	KeysNo += 1
		ListBody .= "Loop .: " A_Index "`tKey . .: " i "`n`tValue .: " Value . "" "`n`n"
		If KeysNo <= %MaxRows%
			ListBody1 := ListBody

		If KeysNo = %MaxRows%
			ListBottom1 := "...`n...`n"
	}
	ListBottomTxt1 := "- - - - - - - - - - - - - - - - - - - -`n"
	ListBottomTxt2 := "All fields is shown above `n`n"
	If ( KeysNo <= MaxRows )
		ListBottom1 .= ListBottomTxt1 ListBottomTxt2
	else
		ListBottom1 .= ListBottomTxt1 "Max " MaxRows " fields is shown ( of " KeysNo " fields ) `n`n"			

	ListBottomTxt3 := 
	( LTrim Join %
		"Table .: " ObjectName "  " ObjectText "`n
		Call from Line .: " JumpFromLine "`n
		Number of keys .: " KeysNo " pcs.`n`n"
	)
	ListBottom1 .= ListBottomTxt3
	Message := ListHead ListBody1 ListBottom1
	
	ClipBoard := ListHead ListBody ListBottomTxt1 ListBottomTxt2 ListBottomTxt3
	
	; Show the result
	MsgBox 64, Row.: %A_LineNumber% -> %A_ScriptName%, % Message "(The result is also available in Clipboard)"
}


ShowMatrix(ArrayName, JumpFromLine)
{	; Version .: 09 feb 2018
	FunctionName = ShowMatrix	; To know the name of this function
	MaxRows = 40	; Maximum number of records as displayed.
	
	; Check and handle if the "ArrayName" is an Object or a String
	If StrLen(ArrayName)
	{	ObjectName = %ArrayName%
		ObjName = %ArrayName%
	}
	else
	{	ObjectName = Unknown
		ObjectText = (The "ArrayName" is not a variable)
		ObjName = ArrayName
	}
	
	If !IsObject( %ObjName% )	; ObjName/(ArrayName) - is not an Array
	{	MsgBox 64, Row.: %A_LineNumber% -> %A_ScriptName%,
		(LTrim Join
		%	"Array / Matrix .: ( " ObjName " ) Does not exist! `n`n
			Function name .: " FunctionName "`n
			Call from line .: " JumpFromLine "`n`n
			This program continues!"
		)
		Return	
	}
	
	; Show the Matrix!
	; MsgBox 64, Row.: %A_LineNumber% -> %A_ScriptName%, % Objectname
	; Count the fields and records!
	For ID1, FieldArray In %ObjName%
	{	If A_Index = 1
		{	For ID2, Fields In FieldArray
				NumberOfFields += 1
		}
		NumberOfRecords += 1
	}
	; MsgBox 64, Row.: %A_LineNumber% -> %A_ScriptName%, % "Number of fields .: "  NumberOfFields "`nNumber of Records .: " NumberOfRecords "`nArrayName.1 .: " ArrayName.1
	

	For ID, FieldArray In %ObjName%
	{	; List1 =
		ListHead := "Function .: " FunctionName "`n"
		ListHead .= "Record .: " A_Index " of " NumberOfRecords "`n`n"
		ListHead .= "----------- KEY ---------- Value --------`n"
		ListHead .= "- - - - - - - - - - - - - - - - - - - - - - - - - -`n"
		; ListHead .= "----------- KEY ---------- Value -----`n"
		
		KeysNo = 0		; Count fields
		For FieldIndex, FieldValue In FieldArray
		{	KeysNo += 1
			ListBody .= "Field: " A_Index "     " FieldIndex "   - - - >   " FieldValue "`n"
			If KeysNo <= %MaxRows%
				ListBody1 := ListBody

			If KeysNo = %MaxRows%
				ListBottom1 := "...`n...`n"
				; Max " MaxRows " fields is shown"
		}
		ListBottomTxt1 := "- - - - - - - - - - - - - - - - - - - - - - - - - -`n"
		ListBottomTxt2 := "All fields is shown above `n`n"
		
		If ( KeysNo <= MaxRows )
			ListBottom1 .= ListBottomTxt1 ListBottomTxt2
		else
			ListBottom1 := ListBottomTxt1 "Max " MaxRows " fields is shown ( of " KeysNo " fields ) `n`n"			
		
		
		ListBottomTxt3 := 
		( LTrim Join %
			"Table .: " ObjectName "  " ObjectText "`n
			Call from Line .: " JumpFromLine "`n`n"
		)
		ListBottom1 .= ListBottomTxt3
		
		Message := ListHead ListBody1 ListBottom1
		ClipBoard := ListHead ListBody ListBottomTxt1 ListBottomTxt2 ListBottomTxt3
		Button := MsgBoxEx(Message "(The result is also available in Clipboard)", "Row.: " A_LineNumber " -> " A_ScriptName, "Next Rec.|Skip Records|Exit AHK", [5, "user32.dll"])
		If ( Button = "Skip Records" )
			Return
		
		If ( Button = "Exit AHK" )
		{	MsgBox 0, %FunctionName%, The program terminates!, 1
			ExitApp
		}
		
		
		
		ListBottom := ""
		ListBottom1 := ""
		ListBody := ""
	}
}

MsgBoxEx(Text, Title := "", Buttons := "", Icon := "", ByRef CheckText := "", Styles := "", Owner := ""
    , Timeout := "", FontOptions := "", FontName := "", BGColor := "", Callback := "")
{	; Version 4 feb 2018
	; From .: Alguimist
	; https://autohotkey.com/boards/viewtopic.php?f=5&t=43140
	; Message box function that supports custom icon.
	; - Utan "Pling"
	; - Med valfri ikon
	; Anrop ex1. .: Result := MsgBoxEx(Text, "Row.: " A_LineNumber " -> " A_ScriptName, "Knapp | Japp | Nopp", [5, "user32.dll"])
	; Anrop ex2. .: Result := MsgBoxEx("Test 1", "Title 1", "OK", [14, "Shell32.dll"])
	; Anrop ex3. .: Result := MsgBoxEx("Test 2", "Title 2", "Yes", [1, "Notepad.exe"])
	; Result - innehåller namnet på den knapp som trycktes (indvik mellanslag)
	;
	; Förbättra med att kontrollera de filer som används till ikoner, finns på rätt ställe!
	; Probrammet behöver inte avbrytas, men ett meddelande kan ges att dessa filer saknas.
	;
	
	Static hWnd, y2, p, px, pw, c, cw, cy, ch, f, o, gL, hBtn, lb, DHW, ww, Off, k, v, RetVal
	Static Sound := {2: "*48", 4: "*16", 5: "*64"}

	Gui New, hWndhWnd LabelMsgBoxEx -0xA0000
	Gui % (Owner) ? "+Owner" . Owner : ""
	Gui Font
	Gui Font, % (FontOptions) ? FontOptions : "s9", % (FontName) ? FontName : "Segoe UI"
	Gui Color, % (BGColor) ? BGColor : "White"
	Gui Margin, 10, 12

    If (IsObject(Icon)) {
        Gui Add, Picture, % "x20 y24 w32 h32 Icon" . Icon[1], % (Icon[2] != "") ? Icon[2] : "shell32.dll"
    } Else If (Icon + 0) {
        Gui Add, Picture, x20 y24 Icon%Icon% w32 h32, user32.dll
        SoundPlay % Sound[Icon]
    }

	Gui Add, Link, % "x" . (Icon ? 65 : 20) . " y" . (InStr(Text, "`n") ? 24 : 32) . " vc", %Text%
	GuicontrolGet c, Pos
	GuiControl Move, c, % "w" . (cw + 30)
	y2 := (cy + ch < 52) ? 90 : cy + ch + 34

	Gui Add, Text, vf -Background ; Footer

	Gui Font
	Gui Font, s9, Segoe UI
	px := 42
	If (CheckText != "")
	{	CheckText := StrReplace(CheckText, "*",, ErrorLevel)
		Gui Add, CheckBox, vCheckText x12 y%y2% h26 -Wrap -Background AltSubmit Checked%ErrorLevel%, %CheckText%
		GuicontrolGet p, Pos, CheckText
		px := px + pw + 10
	}

	o := {}
	Loop Parse, Buttons, |, *
	{	gL := (Callback != "" && InStr(A_LoopField, "...")) ? Callback : "MsgBoxExBUTTON"
		Gui Add, Button, hWndhBtn g%gL% x%px% w90 y%y2% h26 -Wrap, %A_Loopfield%
		lb := hBtn
		o[hBtn] := px
		px += 98
	}
	GuiControl +Default, % (RegExMatch(Buttons, "([^\*\|]*)\*", Match)) ? Match1 : StrSplit(Buttons, "|")[1]

	Gui Show, Autosize Center Hide, %Title%
	DHW := A_DetectHiddenWindows
	DetectHiddenWindows On
	WinGetPos,,, ww,, ahk_id %hWnd%
	GuiControlGet p, Pos, %lb% ; Last button
	Off := ww - (((px + pw + 14) * A_ScreenDPI) // 96)
	For k, v in o
		GuiControl Move, %k%, % "x" . (v + Off)
    
	Guicontrol MoveDraw, f, % "x-1 y" . (y2 - 10) . " w" . ww . " h" . 48

	Gui Show
	Gui +SysMenu %Styles%
	DetectHiddenWindows %DHW%

	If (Timeout)
		SetTimer MsgBoxExTIMEOUT, % Round(Timeout) * 1000

	If (Owner)
		WinSet Disable,, ahk_id %Owner%
    
	GuiControl Focus, f
	Gui Font
	WinwaitClose ahk_id %hWnd%
	Return RetVal
	
	; - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - - - -
	MsgBoxExESCAPE:
	MsgBoxExCLOSE:
	MsgBoxExTIMEOUT:
	MsgBoxExBUTTON:
		SetTimer MsgBoxExTIMEOUT, Delete

        If (A_ThisLabel == "MsgBoxExBUTTON") {
            RetVal := StrReplace(A_GuiControl, "&")
        } Else {
            RetVal := (A_ThisLabel == "MsgBoxExTIMEOUT") ? "Timeout" : "Cancel"
        }
	
		If (Owner)
			WinSet Enable,, ahk_id %Owner%
	
		Gui Submit
		Gui %hWnd%: Destroy
	; - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - - - -
	Return
}
just me
Posts: 5510
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Arrays and Function() - Is this unable?

Post by just me » 10 Feb 2018, 02:42

Code: Select all

Array1 := DataRead(InputData1, A_LineNumber)
Array2 := DataRead(InputData1, A_LineNumber)
How should this create two different arrrays?
Albireo
Posts: 399
Joined: 16 Oct 2013, 13:53

Re: Arrays and Function() - Is this unable?

Post by Albireo » 10 Feb 2018, 05:01

Thank you! :D

Would of course be .:

Code: Select all

Array1 := DataRead(InputData1, A_LineNumber)
Array2 := DataRead(InputData2, A_LineNumber)
Now I also get ByRef to work as I wanted .:

Code: Select all

...
Array1 := DataRead(InputData1, OutHead1, A_LineNumber)
Array2 := DataRead(InputData2, OutHead2, A_LineNumber)
ShowArray(OutHead1, A_LineNumber)
ShowArray(OutHead2, A_LineNumber)
ShowMatrix(Array1, A_LineNumber)
ShowMatrix(Array2, A_LineNumber)
ExitApp

DataRead(InputStr, ByRef Columns,CallFromNo)
{	TableContent := {}
...
I think associative arrays are a bit diffuse. A little difficult to handle these.
- The results can sometimes be difficult to analyze because the key in the arrays is sorted.
- If something is missing or wrong, no results will be generated or the AHK program will crash. (no help - what went wrong)
- My functions ShowArtray() and ShowMatrix() help a little, but I miss something more professional tool to show the structure of the created arrays.
Post Reply

Return to “Ask For Help”