Sequence generator

Post your working scripts, libraries and tools.
william_ahk
Posts: 493
Joined: 03 Dec 2018, 20:02

Sequence generator

Post by william_ahk » 27 Nov 2022, 23:40

I often find myself writing loops for some number sequences, so I wrote this generator. Also, Gui in v2 is amazing! :clap:

Features:
  • Generate numbers from start to stop, increment by step, just like the for-loop.
  • Start can be greater than Stop, you can generate numbers in reverse.
  • Step can be zero, which will make Stop the count of numbers to generate.
  • Template is fed into the Format function, supporting any format option defined in the docs.
screenshot.png
screenshot.png (9.09 KiB) Viewed 1246 times

Code: Select all

#Requires AutoHotkey v2.0-a
#SingleInstance Force
#NoTrayIcon
if FileExist(trayicon_filepath := EnvGet("SystemRoot") "\System32\mmcndmgr.dll")
	TraySetIcon(trayicon_filepath, 48)
MainGui := Gui(, "Series Generator")
MainGui.SetFont("s12", "Courier")
MainGui.Add("Text", "xm", "Start:")
MainGui.Add("Edit", "x+10 w70 section")
MainGui.Add("UpDown", "Range-2147483648-2147483647 vstart")
MainGui.Add("Text", "x+18 vtext_stop", "Stop:")
MainGui.Add("Edit", "x+10 w70")
MainGui.Add("UpDown", "Range-2147483648-2147483647 vstop")
MainGui.Add("Text", "x+18", "Step:")
MainGui.Add("Edit", "x+10 w70")
MainGui.Add("UpDown", "vstep Range0-2147483647")
MainGui.Add("Text", "xm", "Template:")
MainGui.Add("Edit", "x+10 w150 vtemplate")
MainGui.Add("Text", "x+18", "Delimiter:")
MainGui.Add("Edit", "x+10 w60 vdelimiter")

; Default Values
MainGui["start"].value := 1
MainGui["stop"].value := 10
MainGui["step"].value := 1
MainGui["template"].value := "{}"
MainGui["delimiter"].value := "\n"

for ctl in MainGui
	if (ctl.type = "Edit")
		ctl.OnEvent("Change", UpdateState)
MainGui.Add("Edit", "xm r16 w420 voutput")
MainGui.Add("Button", "xm wp", "Copy").OnEvent("Click", (*) => (
	A_Clipboard := MainGui["output"].Text,
	Tooltip("Copied to clipboard"),
	SetTimer(() => ToolTip(), -1500)
))

UpdateState()

MainGui.Show()
MainGui.OnEvent("Close", (*) => ExitApp)
return


UpdateState(*) {
	global MainGui
	start := MainGui["start"].value
	stop := MainGui["stop"].value
	step := MainGui["step"].value
	template := MainGui["template"].value
	delimiter := MainGui["delimiter"].value
	delimiter := delimiter = "\n" ? "`n" : DecodeEscapeChar(delimiter)

	if (step > 0) {
		i := start
		incr := step
		ltr := start < stop
	} else {
		i := 1
		step := 1
		incr := 0
		ltr := true
	}
	if ltr {
		condition := () => (i <= stop)
	} else {
		condition := () => (i >= stop)
		step := -step
		incr := step
	}

	n := start
	output := ""
	while condition() {
		output .= delimiter Format(template, n)
		i += step
		n += incr
	}
	output := SubStr(output, 1+StrLen(delimiter))
	MainGui["output"].value := output
}

DecodeEscapeChar(str) {
	static esc := {n: "`n", r: "`r", b: "`b", t: "`t", v: "`v", a: "`a", f: "`f"}
	output := ""
	str := RegExReplace(str, "(.*?)(\\)(.)(?C)")
	output .= str
	return output

	pcre_callout(m, *) {
		output .= m[1] . (m[3] = "\" ? m[3] : esc.HasOwnProp(m[3]) ? esc.%m[3]% : "")
	}
}

peter_ahk
Posts: 99
Joined: 13 Feb 2024, 14:49

Re: Sequence generator

Post by peter_ahk » 20 Apr 2024, 16:06

very nice, would it be ok if i add this as a tool to my macro lancher in there i am working on a setup gui to batch change button postions and this would be a great tool to include in there to generate a position list

william_ahk
Posts: 493
Joined: 03 Dec 2018, 20:02

Re: Sequence generator

Post by william_ahk » 20 Apr 2024, 19:09

peter_ahk wrote:
20 Apr 2024, 16:06
very nice, would it be ok if i add this as a tool to my macro lancher in there i am working on a setup gui to batch change button postions and this would be a great tool to include in there to generate a position list
Of course :)

peter_ahk
Posts: 99
Joined: 13 Feb 2024, 14:49

Re: Sequence generator

Post by peter_ahk » Yesterday, 02:25

hello william could you tell me how to make it alwaysontop, i cant say i understand v2 code :)

william_ahk
Posts: 493
Joined: 03 Dec 2018, 20:02

Re: Sequence generator

Post by william_ahk » Yesterday, 02:57

Like so MainGui := Gui("+AlwaysOnTop", "Series Generator")

peter_ahk
Posts: 99
Joined: 13 Feb 2024, 14:49

Re: Sequence generator

Post by peter_ahk » Yesterday, 02:59

william_ahk wrote:
Yesterday, 02:57
Like so MainGui := Gui("+AlwaysOnTop", "Series Generator")
awesome thank you william :thumbup:

User avatar
kunkel321
Posts: 1056
Joined: 30 Nov 2015, 21:19

Re: Sequence generator

Post by kunkel321 » Yesterday, 15:04

Pretty cool thing! I'm playing around with the Template: value... I don't really understand what is happening. I looked in the code, then found my way to this
https://www.autohotkey.com/docs/v2/lib/Format.htm

I'm just experimenting with some of the values in that ahk docs page...
If I set the Stop to 250, then set template to {1:2x} then I get what looks like a base-16 incrementation.
Spoiler
How is {1:2x} resulting in this?
ste(phen|ve) kunkel

william_ahk
Posts: 493
Joined: 03 Dec 2018, 20:02

Re: Sequence generator

Post by william_ahk » Yesterday, 19:21

william_ahk wrote:
27 Nov 2022, 23:40
Template is fed into the Format function, supporting any format option defined in the docs.
@kunkel321 I have mentioned this in the post :D

For the format string you tested, the syntax of the first parameter of Format is {Index:Format}. Index can only be 1 in this case as only a single value is provided to the function. It can be omitted if only one placeholder is used. Now to the other side - format specifiers,
  1. A decimal integer indicates the "width", or padding of the string;
    By default, values are right-aligned and spaces are used for padding.
    Format - Syntax & Usage
  2. A character from the type table indicates the type of the value, x means hexadecimal.
So :2x means, left pad 2 spaces and transform the value to hex.
I mainly use this to add leading zeroes by prefixing 0 to the width specifier, like so :02, producing the following:
01
02
03
04
05
06
07
08
09
10
There are of course many more possibilities with this function :trollface:

User avatar
kunkel321
Posts: 1056
Joined: 30 Nov 2015, 21:19

Re: Sequence generator

Post by kunkel321 » Yesterday, 20:13

william_ahk wrote:
Yesterday, 19:21
@kunkel321 I have mentioned this in the post :D
Ah yes, I see that now -- LOL!
And thanks for the extended explanation!
ste(phen|ve) kunkel

Post Reply

Return to “Scripts and Functions (v2)”