Format built-in variable (A_Index) to always include at least four digits

Get help with using AutoHotkey and its commands and hotkeys
WeThotUWasAToad
Posts: 209
Joined: 19 Nov 2013, 08:44

Format built-in variable (A_Index) to always include at least four digits

16 Oct 2018, 21:25

Hello,

What command will force the output variable to include a specified number of digits regardless of its value?

For example, suppose you want to enter a list of numbers in Notepad using the following script:

Code: Select all

F1::
Loop, 12
{
Var:=A_Index
Send %Var%
Send {Enter}
}
Return
As currently written, the script returns:
1
2
3
4
5
6
7
8
9
10
11
12
But suppose instead, you want the following result:
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
What change in the script will produce that?

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word — it never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
User avatar
jeeswg
Posts: 5275
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Format built-in variable (A_Index) to always include at least four digits

16 Oct 2018, 21:42

Cheers.

Code: Select all

vNum := 12
MsgBox, % Format("{:04}", vNum)
WeThotUWasAToad
Posts: 209
Joined: 19 Nov 2013, 08:44

Re: Format built-in variable (A_Index) to always include at least four digits

16 Oct 2018, 22:13

Thanks jeeswg.

I suspected it was something like that because I was looking at that page (Format) in the docs but it seems to indicate that you have to enter a "Type Character" which had me confused.

Apparently I'm still confused however because after inserting your syntax in the full script I'm using (see below), it returns:
-18_::::
-18_::::
-18_::::
-18_::::
-18_::::
-18_::::
Rather than what I'm trying to get:
0002
0003
0004
0005
0006
0007

Code: Select all

F1::
;Sleep lengths:
    short := 299
    medium := 999
    long := 2299
    date  := 18
    base := 1
Loop, 6
{
Send {Home}
	Sleep, short
Send {Right 7}
	Sleep, short
Send -
	Sleep, short
Send %date%
	Sleep, short
Send _
	Sleep, short
var:= base + A_Index
	Sleep, short
;Send %var%
Send % Format ("{: 04}", var)
	Sleep, short
Send {Enter}	;{Tab}
	Sleep, short
}
Return
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word — it never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
WeThotUWasAToad
Posts: 209
Joined: 19 Nov 2013, 08:44

Re: Format built-in variable (A_Index) to always include at least four digits

16 Oct 2018, 22:16

Correction:

This is what I expected:
-18_0002
-18_0003
-18_0004
-18_0005
-18_0006
-18_0007
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word — it never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
User avatar
SpeedMaster
Posts: 235
Joined: 12 Nov 2016, 16:09

Re: Format built-in variable (A_Index) to always include at least four digits

17 Oct 2018, 02:16

Apparently I'm still confused ...

Syntax error... :|

Code: Select all

var := 1 

msgbox,  % Format ("{: 04}", var)      "`n`n expression without any function call"

msgbox,  % format("{: 04}", var)       "`n`n expression with a function call"

return
Cheers

Return to “Ask For Help”

Who is online

Users browsing this forum: Mipha, wryyymuda and 51 guests