Reading letters and numbers

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SkrillexAkaCraft
Posts: 119
Joined: 25 Dec 2015, 10:01

Reading letters and numbers

23 Sep 2017, 20:42

So i am trying to make a script for myself which automatically creates coupon codes so what it needs to do is form a certain word with numbers behind it and place it down in a .txt so that it is uable to generate the same code again
User avatar
Spawnova
Posts: 554
Joined: 08 Jul 2015, 00:12
Contact:

Re: Reading letters and numbers

23 Sep 2017, 21:20

You want to create your own codes? if so this should get you started

Code: Select all

global words := ["red","blue","green","one","two","three"]
global minNumbers := 4
global maxNumbers := 4

iniread,currentCode,coupon.ini,code,current,0 ;current code is stored in coupon.ini
if (currentCode == 0) {
	currentCode := generateCode(0) ;only happens when first run to generate a starting code
	iniwrite,%currentCode%,coupon.ini,code,current
}
gui,+toolwindow
gui,font,s20 cFF0000
Gui, Add, Text, x2 y8 w130 h52 vmainText, % currentCode
gui,font
Gui, Add, Button, x2 y60 w130 h30 gnewCode, New Code
Gui, Show, x127 y87 h92 w138, Coupon Codes
Return

GuiClose:
ExitApp

newCode:
currentCode := generateCode(currentCode)
iniwrite,%currentCode%,coupon.ini,code,current
guicontrol,,mainText,% currentCode
return

generateCode(currentCode) {
	RegExMatch(currentCode,"^(\w+)(\d+)",match)
	currentWord := (match ? match1 : "")
	currentNumber := (match ? match2 : "")
	loop {
		word := words[random(1,words.length())]
		if (word != currentWord) ;dont use the last word
			break
	}
	loop {
		numDigits := Random(minNumbers,maxNumbers)
		loop % numDigits
			numbers .= random(0,9)
		if (numbers != currentNumber) ;dont use the last digits
			break
	}
	return word numbers
}

random(min,max=100) {
	random,result,min,max
	return result
}
SkrillexAkaCraft
Posts: 119
Joined: 25 Dec 2015, 10:01

Re: Reading letters and numbers

24 Sep 2017, 13:15

Spawnova wrote:You want to create your own codes? if so this should get you started

Code: Select all

global words := ["red","blue","green","one","two","three"]
global minNumbers := 4
global maxNumbers := 4

iniread,currentCode,coupon.ini,code,current,0 ;current code is stored in coupon.ini
if (currentCode == 0) {
	currentCode := generateCode(0) ;only happens when first run to generate a starting code
	iniwrite,%currentCode%,coupon.ini,code,current
}
gui,+toolwindow
gui,font,s20 cFF0000
Gui, Add, Text, x2 y8 w130 h52 vmainText, % currentCode
gui,font
Gui, Add, Button, x2 y60 w130 h30 gnewCode, New Code
Gui, Show, x127 y87 h92 w138, Coupon Codes
Return

GuiClose:
ExitApp

newCode:
currentCode := generateCode(currentCode)
iniwrite,%currentCode%,coupon.ini,code,current
guicontrol,,mainText,% currentCode
return

generateCode(currentCode) {
	RegExMatch(currentCode,"^(\w+)(\d+)",match)
	currentWord := (match ? match1 : "")
	currentNumber := (match ? match2 : "")
	loop {
		word := words[random(1,words.length())]
		if (word != currentWord) ;dont use the last word
			break
	}
	loop {
		numDigits := Random(minNumbers,maxNumbers)
		loop % numDigits
			numbers .= random(0,9)
		if (numbers != currentNumber) ;dont use the last digits
			break
	}
	return word numbers
}

random(min,max=100) {
	random,result,min,max
	return result
}
so in the " global words :=" part i have to fill in all numbers and letters i know off?
User avatar
Spawnova
Posts: 554
Joined: 08 Jul 2015, 00:12
Contact:

Re: Reading letters and numbers

24 Sep 2017, 14:54

Only the words, numbers are automatically set, the length of numbers is determined by minNumbers and maxNumbers variable.

if you need a very large list of words, it may be easier to save them to a .txt file and read them from ahk.
example:

Code: Select all

global words := []
global minNumbers := 3 ;numbers must be atleast 3 digits long
global maxNumbers := 4 ;number must not be over 4 digits long

fileread,wordList,wordList.txt ;load text file into memory (worldList.txt)
loop,parse,wordList,`n,`r ;parse through the text, checking every new line
	words.insert(a_loopfield) ;insert word into array

iniread,currentCode,coupon.ini,code,current,0 ;current code is stored in coupon.ini
if (currentCode == 0) {
	currentCode := generateCode(0) ;only happens when first run to generate a starting code
	iniwrite,%currentCode%,coupon.ini,code,current
}
gui,+toolwindow
gui,font,s14 cFF0000
Gui, Add, Text, x2 y14 w130 h46 vmainText, % currentCode
gui,font
Gui, Add, Button, x2 y60 w130 h30 gnewCode, New Code
Gui, Show, x127 y87 h92 w138, Coupon Codes
Return

GuiClose:
ExitApp

newCode:
currentCode := generateCode(currentCode)
iniwrite,%currentCode%,coupon.ini,code,current
guicontrol,,mainText,% currentCode
return

generateCode(currentCode) {
	RegExMatch(currentCode,"^(\w+)(\d+)",match)
	currentWord := (match ? match1 : "")
	currentNumber := (match ? match2 : "")
	loop {
		word := words[random(1,words.length())]
		if (word != currentWord) ;dont use the last word
			break
	}
	loop {
		numDigits := Random(minNumbers,maxNumbers)
		loop % numDigits
			numbers .= random(0,9)
		if (numbers != currentNumber) ;dont use the last digits
			break
	}
	return word numbers
}

random(min,max=100) {
	random,result,min,max
	return result
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 336 guests