Help me to understand Format method Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DanielToward13
Posts: 74
Joined: 18 May 2017, 10:56

Help me to understand Format method

19 May 2017, 06:56

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)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help me to understand Format method

19 May 2017, 08:30

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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
DanielToward13
Posts: 74
Joined: 18 May 2017, 10:56

Re: Help me to understand Format method

19 May 2017, 11:20

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?
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: Help me to understand Format method

19 May 2017, 12:16

'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
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help me to understand Format method

19 May 2017, 12:33

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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
DanielToward13
Posts: 74
Joined: 18 May 2017, 10:56

Re: Help me to understand Format method

20 May 2017, 02:20

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?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help me to understand Format method  Topic is solved

20 May 2017, 05:56

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.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RussF and 370 guests