Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

String-object-file (data structures in YAML-like style)


  • Please log in to reply
11 replies to this topic
Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
WARNING: This is old thread. It is continued here.
String-object-file%20preview.png

How do you (and/or your users) prefer to create, maintain and store complex objects? What's your style?
If you prefer image on the:
- right, than there's nothing interesting for you in this topic :(
- left, try this out B)
String-object-file%20preview%202.png

Download String-object-file.ahk, which includes 6 examples, Documentation, License, See also (links),Tips, and StrObj() code of course.
Usage is extremely simple - Just call StrObj() function and it will automatically conclude what do you want to do, depending on type of your input. One function does it all. B)

Object := StrObj(String) ; String to Object (constructs object from string)
String := StrObj(Obj) ; Object to String (converts object to string)
Object := StrObj(File) ; File to Object (constructs object from file)
ErrorLevel := StrObj(Obj,OutFile) ; Saves Object to File


See also:
- Yaml Parser (++JSON) by HotKeyIt
- JSON module by Coco
- JSON-like (de)serializer by VxE
- Json <---> Object by lordkrandel
- Object from/to file or string by Learning one (obsolete)

My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy


Sanctus
  • Members
  • 283 posts
  • Last active: Dec 06 2014 04:05 PM
  • Joined: 30 Nov 2012

IniWrite and IniRead, very simple concept that works for usually anything that you need to save outside the script for later use.

 

http://www.autohotke...ds/IniWrite.htm


Check out ALL My Scripts  ;)


Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009

Ini files are good for simple things, but you can't create/save/load/edit complex data structures with Ini files easily. ;) That's why I think it's better to use this, or pure YAML, or JSON when you have to deal with complex data structures.


My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy


Sanctus
  • Members
  • 283 posts
  • Last active: Dec 06 2014 04:05 PM
  • Joined: 30 Nov 2012

Ini files are good for simple things, but you can't create/save/load/edit complex data structures with Ini files easily. ;) That's why I think it's better to use this, or pure YAML, or JSON when you have to deal with complex data structures.

True, but I have yet had the need for complex data structures :p


Check out ALL My Scripts  ;)


Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009

@Sanctus: OK ;)

* * *

StrObj updated to version 1.01

  • improved support for having Indent and NewLine characters in keys & values (by defualt; `t and `n)
  • faster performance in some cases
  • added more info in comments and 1. post
  • added new example

Spoiler

My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy


bichlepa
  • Members
  • 62 posts
  • Last active: Dec 17 2016 08:45 PM
  • Joined: 04 Jul 2011

Well, I have complex data structures in my new project  AutoHotFlow  ;).

Thank you very much for that script. I've included it and now AutoHotFlow can save and get global objects.



Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009

I'm glad you found it useful ;)


My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy


Gicu
  • Members
  • 3 posts
  • Last active: Jul 27 2015 07:47 AM
  • Joined: 19 Jun 2015
hi!
is it normal?
it trim zeros and spaces when read same obj from file
 
old_obj := ["00034451608", "00001", "some text before space  ", " AAA ", "000"]

MsgBox % strobj(old_obj)
MsgBox % "<" old_obj.3 ">" "<" old_obj.4 ">" 

strobj(old_obj, A_Desktop "\test.txt")

new_obj := strobj(A_Desktop "\test.txt")
MsgBox % strobj(new_obj)
MsgBox % "<" new_obj.3 ">" "<" new_obj.4 ">" 
and from string
 
yaml=
(
- 00034451608
- 00001
- Text  
-  AAA 
- 000
)

obj := strobj(yaml)
MsgBox % strobj(obj)
ehm sorry for my english

T_Lube
  • Members
  • 640 posts
  • Last active: Sep 09 2016 02:19 AM
  • Joined: 16 Oct 2014

I think that this is pretty cool. may I also recommend this idea for ini files into and out of arrays, just a small lib I put together, could also be used with this StrObj library:


arrayIni_S(Path, Section) {
	oSection := Object() ; init
	If (!SectionFull := ini_R(Path, Section))
		Return 0
	Loop, Parse, SectionFull, `n		;Key := SubStr(A_LoopField, 1, (SetPos := InStr(A_LoopField, "="))-1)
		oSection[SubStr(A_LoopField, 1, (SetPos := InStr(A_LoopField, "="))-1)] := SubStr(A_LoopField, SetPos+1)
	Return oSection
}

arrayIni(iniPath, Section="") { ; returns an array object for each section within a overall array
	If !FileExist(iniPath)
		Return
	If (Section)
		Return arrayIni_S(iniPath, Section)
	iniFile := Object().Path := iniPath
	Loop, Read, %iniPath%
		Heads .= (RegExMatch(A_LoopReadLine, "^\[.*\]$") ? (Heads ? "`n" : "") . RegExReplace(A_LoopReadLine, "[\[\]]") : "")
	Loop, Parse, Heads, `n
		IniFile[A_LoopField] := arrayIni_S(iniPath, A_LoopField)
	iniFile.Sections := Heads
	Return iniFile
}

Ini_R(Filename, Section, Key="", Default="") {
	IniRead, v, %Filename%, %Section%, %Key%, %Default%
	Return, v
}

Ini_W(Value, File, Section, Key="")
{
	IniWrite, %Value%, %File%, %Section%, %Key%
	Return Abs(ErrorLevel-1)
}

iniArray(Array, Path="") { ; writes an array to an ini file section by section, the inverse for this is "arrayIni"
	NewArray := Array
	If !Path
		Path := NewArray.Path
	For K, SectionArray in Array
	{
		If !IsObject(SectionArray)
			Continue
		For Key, Val in SectionArray
			Ini_W(Val, Path, K, Key)
	}
	NewArray.Path := Path
	Return NewArray
}


Gicu
  • Members
  • 3 posts
  • Last active: Jul 27 2015 07:47 AM
  • Joined: 19 Jun 2015

another problem (?)
 

; Key is number, problem
key := 999	
obj := []	
temp := []
temp["A"] := "Value"
obj[key] := temp

result := StrObj(obj)

MsgBox, 0, Key is number, % result
	. "`n------------------------------------"
	. "`nkey ""999"" --> " obj.999.A
	. "`nkey ""1"" --> " obj.1.A

obj := StrObj(result)
result := StrObj(obj)

MsgBox, 0, Key is number, % result
	. "`n------------------------------------"
	. "`nkey ""999"" --> " obj.999.A
	. "`nkey ""1"" --> " obj.1.A
	
; Key is string, ok
key := "Z"
obj := []
temp := []
temp["A"] := "Value"
obj[key] := temp

result := StrObj(obj)

MsgBox, 0, Key is string, % result
	. "`n------------------------------------"
	. "`nkey ""Z"" --> " obj.Z.A

obj := StrObj(result)
result := StrObj(obj)

MsgBox, 0, Key is string, % StrObj(obj)
	. "`n------------------------------------"
	. "`nkey ""Z"" --> " obj.Z.A


Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009

@Gicu: Thanks for report. :) It intetionally trims spaces because of this line;

k := Trim(k, " `t`r"), v := Trim(v, " `t`r") ; k=key, v=value

Zeros are trimmed because of this line;

if value is Integer
	value := value*1 ; assigns a pure number instead of string - important for some COM methods

I'm too busy at the moment so I'll try to improve and fix that later...

@T_Lube: Nice! :)


My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy


Gicu
  • Members
  • 3 posts
  • Last active: Jul 27 2015 07:47 AM
  • Joined: 19 Jun 2015

thanks to you, your class helped me a lot to understand objects/multidimensional objects structure!