Page 1 of 1

Help me to understand Format method

Posted: 19 May 2017, 06:56
by DanielToward13
I have tried a lot to understand the below code but couldn't figure it out. Could you please explain the code for me.

Code: Select all

parsed_out := Format("
(Join`r`n
String: {}
Number: {}
Float:  {}
true:   {}
false:  {}
null:   {}
array:  [{}, {}, {}]
object: {{}A:""{}"", H:""{}"", K:""{}""{}}
)"
, parsed.str, parsed.num, parsed.float, parsed.true, parsed.false, parsed.null
, parsed.array[1], parsed.array[2], parsed.array[3]
, parsed.object.A, parsed.object.H, parsed.object.K)

Re: Help me to understand Format method

Posted: 19 May 2017, 08:30
by jeeswg
Do you have a link for that? Basically the value for the (n+1)th parameter of Format should appear in the nth set of curly brackets.

Code: Select all

q::
MsgBox, % Format("{} {} {}", 1, 2, 3)
return
I have many Format examples here:
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486

Re: Help me to understand Format method

Posted: 19 May 2017, 11:20
by DanielToward13
jeeswg wrote:Do you have a link for that? Basically the value for the (n+1)th parameter of Format should appear in the nth set of curly brackets.

Code: Select all

q::
MsgBox, % Format("{} {} {}", 1, 2, 3)
return
I have many Format examples here:
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486
The source code is JSON library example:
https://github.com/cocobelgica/AutoHotk ... e_JSON.ahk

What is the function of Join? When we need to use Format method?

Re: Help me to understand Format method

Posted: 19 May 2017, 12:16
by garry
'join' is the end of line character ( in this case `r`n )
easy example for format ( left or right align ) ( used non proportional script )

Code: Select all

file=
(join`r`n
a1234,b1234,c12345678
a12345,b12345678,c12345
)
Gui,2:Font,,FixedSys
Gui,2:Add, Edit,vA1x w600 h100
Gui,2:Show, ,Test
gosub,a1
return
2Guiclose:
exitapp

a1:
Loop, parse, file, `n,`r
 {
 a:=StrSplit(A_LoopField,",")
 newline .= Format("{:7}|{:10}|{:-10}`r`n",a*)
 }
GuiControl,2:,A1x,%newline%
return

Re: Help me to understand Format method

Posted: 19 May 2017, 12:33
by jeeswg
I knew I saw this somewhere recently, I was looking at JSON, I couldn't find it via Google.

You've found the most complicated ever example of Format.

The 'Join' relates to continuation sections, which is used (for example) to create a WYSIWYG string over multiple lines.

AutoHotkey Scripts and Macros
https://autohotkey.com/docs/Scripts.htm#continuation

Btw:
Format
https://autohotkey.com/docs/commands/Format.htm
Use {{} and {}} to include literal braces in the string. Any other invalid placeholders are included in the result as is.
That script (the JSON library example) takes a string in JSON format (similar to the ini file format but more versatile), and from that creates an object.

I've replicated the object that the script creates. The script then shows the output text of Format, and then the continuation section text by itself.

Code: Select all

q:: ;Format and continuation section example
parsed := {}
parsed.str := "Hello World"
parsed.num := 12345
parsed.float := 123.5
parsed.true := true
parsed.false := false
parsed.null := null
parsed.array := ["Auto","Hot","key"]
parsed.object := {A:"Auto",H:"Hot",K:"key"}

parsed_out := Format("
(Join`r`n
String: {}
Number: {}
Float:  {}
true:   {}
false:  {}
null:   {}
array:  [{}, {}, {}]
object: {{}A:""{}"", H:""{}"", K:""{}""{}}
)"
, parsed.str, parsed.num, parsed.float, parsed.true, parsed.false, parsed.null
, parsed.array[1], parsed.array[2], parsed.array[3]
, parsed.object.A, parsed.object.H, parsed.object.K)
parsed := ""

MsgBox, % parsed_out

MyContinuationSection := "
(Join`r`n
String: {}
Number: {}
Float:  {}
true:   {}
false:  {}
null:   {}
array:  [{}, {}, {}]
object: {{}A:""{}"", H:""{}"", K:""{}""{}}
)"
MsgBox, % MyContinuationSection
return

Re: Help me to understand Format method

Posted: 20 May 2017, 02:20
by DanielToward13
Thanks jeeswg. I understand the format method now but why the programmer used Join? When I delete it, the result is same. Join`r`n is adding a new line to the end of each retrieved result?

Re: Help me to understand Format method  Topic is solved

Posted: 20 May 2017, 05:56
by jeeswg
Oh I see, the result is the same, if you remove Join, except that the text is LF-delivered rather than CRLF-delimited. Join determines what string is put between each line, it's one of various options that can be used with continuation sections.