Replace letter with it's number Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
IvanVanko
Posts: 34
Joined: 14 Jul 2016, 11:40

Replace letter with it's number

22 Sep 2016, 08:34

How to do a script that would replace a letter with it's number (a - 01; b - 02; c-03 and so on)? Should I use Loop? I know there is a hard way to do it one-by-one with pure StringReplace, but copy-paste is a very dirty and awful thing to do in code.
Any help would be appreciated!
Твой софт - говно!
ahkForWork
Posts: 92
Joined: 28 Mar 2016, 07:59

Re: Replace letter with it's number

22 Sep 2016, 08:43

depends on how you want it to happen, hotkey? triggered by input into an edit field? either way, the below will help start your brain.

Code: Select all

ALPHA_NUMBERS := "A:01,B:02,C:03,D:04,E:05,F:06,G:07,H:08,I:09,J:10,K:11,L:12,M:13,N:14,O:15,P:16,Q:17,R:18,S:19,T:20,U:21,V:22,W:23,X:24,Y:25,Z:26"
inputVar := "D"
loop parse,  ALPHA_NUMBERS, `,
{
	StringSplit array, A_LoopField, `:
	if (inputVar = array1) {
		MsgBox % "the number is " array2
		break
	}
}
ExitApp
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: Replace letter with it's number

22 Sep 2016, 09:11

I would do it like this.

Code: Select all

MsgBox, % CharToNum("abcdefg")
MsgBox, % CharToNum("ABCDEFG")
CharToNum(String){
	a := Ord("a") - 1
	for index, char in StrSplit(String){
		res .= Format("{:02}", Ord(Format("{:L}", char)) - a)
	}
	return res
}
Please excuse my spelling I am dyslexic.
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Replace letter with it's number  Topic is solved

22 Sep 2016, 09:50

another example

Code: Select all

a=ABCZYX
loop,parse,a,
Z .= substr(0 Asc(A_LoopField)-64,-1)
msgbox,%z%
User avatar
IvanVanko
Posts: 34
Joined: 14 Jul 2016, 11:40

Re: Replace letter with it's number

22 Sep 2016, 14:23

Dammit, you guys are awesome! Thank you everyone!
P. S. I like Garry's example more than others as it's super simple
Твой софт - говно!
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Replace letter with it's number

22 Sep 2016, 14:51

user 'Capn Odin' considered Upper /Lower
so script also not so short ....

Code: Select all

a=AbCzYX
loop,parse,a,
{
if (asc(a_loopfield) > 64 and asc(a_loopfield) < 91)
   Z .= substr(0 Asc(A_LoopField)-64,-1)
if (asc(a_loopfield) > 96 and asc(a_loopfield) < 123)
   Z .= substr(0 Asc(A_LoopField)-96,-1)
}
msgbox,%z%
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: fiaztv1, JKJadan, Pianist and 198 guests