object reference in .ini files Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Lu3d
Posts: 7
Joined: 09 Oct 2016, 14:45
Contact:

object reference in .ini files

27 Feb 2017, 15:25

Hello everybody! This is my first post, and its good to be here with all you people. I would like to learn all I can, and also give back.

I am a nooby in ahk and in programming generally, also in forum usage. I made this snippet of code which works but irritates me because I know there's a better way of doing it. :think:

I'm wondering if somebody could help me...

Code: Select all

				If Instr(sectionkey.2,".") { 	 	; If the value looks like a reference to an existing object....
					For key4 in StrSplit(sectionkey.2,".") {
						vvv%A_Index% := StrSplit(sectionkey.2,".")[A_Index]  ;  parse out the reference so we can find it.
					}
					Try {  ;  We're going to go seven deep.  I know there is a better way of doing this, but I haven't been able to figure it out...
						%val%[(sectionkey.1)] := %vvv1%[(vvv2)] 
							? %vvv1%[(vvv2)] 
							: %val%[(sectionkey.1)]
						%val%[(sectionkey.1)] := %vvv1%[(vvv2),(vvv3)] 
							? %vvv1%[(vvv2),(vvv3)] 
							: %val%[(sectionkey.1)]
						%val%[(sectionkey.1)] := %vvv1%[(vvv2),(vvv3),(vvv4)] 
							? %vvv1%[(vvv2),(vvv3),(vvv4)] 
							: %val%[(sectionkey.1)]
						%val%[(sectionkey.1)] := %vvv1%[(vvv2),(vvv3),(vvv4),(vvv5)] 
							? %vvv1%[(vvv2),(vvv3),(vvv4),(vvv5)] 
							: %val%[(sectionkey.1)]
						%val%[(sectionkey.1)] := %vvv1%[(vvv2),(vvv3),(vvv4),(vvv5),(vvv6)] 
							? %vvv1%[(vvv2),(vvv3),(vvv4),(vvv5),(vvv6)] 
							: %val%[(sectionkey.1)]
						%val%[(sectionkey.1)] := %vvv1%[(vvv2),(vvv3),(vvv4),(vvv5),(vvv6),(vvv7)] 
							? %vvv1%[(vvv2),(vvv3),(vvv4),(vvv5),(vvv6),(vvv7)] 
							: %val%[(sectionkey.1)]
					}
				}
%val% is the section heading(s) for the ini file. As the objects are loaded from the ini file, if there is a previous object referenced, it will load it's value instead.
For example:

Code: Select all

[Path]
need = \\172.25.2.2\Settings\Backup\OOP\ControlFiles\
log = path.need
and path.log = \\172.25.2.2\Settings\Backup\OOP\ControlFiles\

Could anybody help me with this multi-dimensional key stuff?
Lu3d
Posts: 7
Joined: 09 Oct 2016, 14:45
Contact:

Re: object reference in .ini files  Topic is solved

11 Jun 2018, 14:40

For anybody that is interested in this post, I was able to figure out how to do this.

Here is the function:

Code: Select all

HTFn_GetValue(para_var) {
	For vkey, vval in StrSplit(para_var,".") {
		temp_obj2 := IsObject(temp_obj) ? temp_obj[vval] : %vval%
		If IsObject(temp_obj2)
			temp_obj := temp_obj2
		else
			Return temp_obj2
	}
	Return temp_obj
}
The function has the ability to return values and objects, and works way better for what I am doing.
It only translates objects out of strings using period separated keys. It could be modified easily to recognize other object formats, but I didn't need that.
Here is an example:

Code: Select all

help := {state:{rabbit:{moose:"chicken"}}} ; create a muti-dimensional object
InColor := "Green"
MsgBox % HTFn_GetValue("help.state.rabbit.moose") ; Returns "chicken".
MsgBox % HTFn_GetValue("InColor") ; Returns "Green".
GUIObject(HTFn_GetValue("help.state")) ; Returns object help.state



GUIObject(Array, Arraytext := "Object", Parent="") { ; I'm not sure who built this function, but it is very useful; thank you whoever you are
	static
	global GuiArrayTree, GuiArrayTreeX, GuiArrayTreeY
	if Array_IsCircle(Array)
	{
		MsgBox, 16, GuiObject, Error: Circular reference
		return "Error: Circular reference"
	}
	if !Parent
	{
		Gui, +HwndDefault
		Gui, GuiArray:New, +HwndGuiArray +LabelGuiArray +Resize
		Gui, Add, TreeView, vGuiArrayTree
		Parent := "P1"
		%Parent% := TV_Add(Arraytext, 0, "+Expand")
		GUIObject(Array, Arraytext, Parent)
		GuiControlGet, GuiArrayTree, Pos
		Gui, Show,, GuiArray
		Gui, %Default%:Default
		WinWaitActive, ahk_id%GuiArray%
		WinWaitClose, ahk_id%GuiArray%
		return
	}
	Try
		For Key, Value in Array
		{
			%Parent%C%A_Index% := TV_Add(Key, %Parent%)
			KeyParent := Parent "C" A_Index
			if (IsObject(Value))
				GUIObject(Value, Arraytext, KeyParent)
			else {
				%KeyParent%C1 := TV_Add(Value, %KeyParent%)
			}
		}
	return
	GuiArrayClose:
	Gui, Destroy
	return
	GuiArraySize:
	if !(A_GuiWidth || A_GuiHeight) ; Minimized
		return
	GuiControl, Move, GuiArrayTree, % "w" A_GuiWidth - (GuiArrayTreeX * 2) " h" A_GuiHeight - (GuiArrayTreeY * 2)
	return
}

Array_IsCircle(AObj, AObjs=0) {
	if !AObjs
		AObjs := {}
	Try
		For Key, Val in AObj
			if (IsObject(Val)&&(AObjs[&Val] || Array_IsCircle(Val,(AObjs,AObjs[&Val]:=1))))
				return 1
		return 0
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo and 217 guests