Next iteration in alphabetical list

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
flw
Posts: 5
Joined: 14 Nov 2017, 04:00

Next iteration in alphabetical list

29 Jul 2019, 04:25

Hi,

I would like to find the next iteration of an entry in an alphabetical list. For example, if I have a string

Code: Select all

;if RegExMatch(vReferenceOriginal, "(\D+)$", vEndNonDigit) is not 0
vEndNonDigit := "z"
the output should be string

Code: Select all

 vEndNonDigitNew := "aa"
The list would be:

Code: Select all

a
b
c
...
z
aa
ab
ac
...
az
aaa
...
zzz
Can someone help me?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Next iteration in alphabetical list

29 Jul 2019, 05:07

Try this:

Code: Select all

    ; make a List array from given text
    FileRead, content, List.txt
    global List := StrSplit(content, "`n", "`r")
    
    ; Test
    MsgBox, % Next("z")

return ; end of auto-execute section



;-------------------------------------------------------------------------------
Next(Item) { ; return the follower of item in the list}
;-------------------------------------------------------------------------------

    for i, entry in List
        if (entry = item)
            return List[i+1]
}
I hope that helps.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Next iteration in alphabetical list

29 Jul 2019, 05:23

That list doesn't seem very alphabetical to me.
First you go from a to z and then from aa to zz.
Most lists go from a-aa-aaa to z-zz-zzz.
If you aren't picky about using either this could be the shortest solution for your specific request:

Code: Select all

list := [] ;an array of strings not sure what else you mean by list
list.push("Hello World")
list.push("a")
list.push("b")
list.push("zz")
list.push("aa")

sortedList := turnListIntoSortedList(list)

currentEntry := "a"
Loop
	Msgbox % currentEntry := sortedList[currentEntry]

turnListIntoSortedList(listArray) {
	sortedList := {}
	for each, entry in listArray {
		sortedList[entry] := "" ;will automatically sort
	}
	for entry, value in sortedList {
		if (A_Index = 1) {
			firstEntry := entry
		} else {
			sortedList[lastEntry] := entry
			if (A_Index = sortedList.count()) {
				sortedList[entry] := firstEntry
			}
		}
		lastEntry := entry
	}
	return sortedList
}
Recommends AHK Studio
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Next iteration in alphabetical list

29 Jul 2019, 05:42

Code: Select all

vEndNonDigit := "z"
RegExMatch(list, vEndNonDigit "[\r\n]+([^\r\n]+)",o)
MsgBox % o1
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
flw
Posts: 5
Joined: 14 Nov 2017, 04:00

Re: Next iteration in alphabetical list

29 Jul 2019, 05:55

Thank you for your contributions. I worked some more on my list and can generate msgboxes with the correct order, however I cannotfill the array with the .push in the loops. Can someone show me the bug? Afterwards I should be able to find a given letter ("g") or letter combination ("ab") in the array and output the next entry, right?

Code: Select all

arrayList := []

Loop, 26 {
	var := Chr(A_Index + 96)
	 arrayList.Push(%var%)
	 ;MsgBox, % var
	 }

Loop 26 {
	var2:= Chr(A_Index + 96)
	Loop, 26 {
		var := var2 Chr(A_Index + 96)
		arrayList.Push(%var%)
		;MsgBox, % var
		}
	}	

Last_item:=arrayList%Array0%  
MsgBox, Last item = %Last_item%
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Next iteration in alphabetical list

29 Jul 2019, 07:41

This looks like you are mixing up a few things.
1 - don't use legacy syntax in function calls. Use Function syntax (No %)
2 - don't use legacy array when you got real ones.
3 - to get real array, you must create one before the Push()

Code: Select all

arrayList := []
Edit: oop, nr 3 was not a mix-up, my bad
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Next iteration in alphabetical list

30 Jul 2019, 04:04

When you are creating that list in the same way like that every time it's easier if you do not actually save a list.

If you assign numbers to a combination of letters you can then turn the letter into a number and back.
To get the next entry -> turn into number -> add 1 -> turn the new number into a combination of letters:

In your case you could probably use something like:

Code: Select all

turnNumberIntoLetters(Nr, wordLength := 3) { ;starting with 0
	Nr := mod(Nr, (26**wordLength))
	out := ""
	nrWordLenght := ceil(log(Nr) / log(26))
	Loop % nrWordLength {
		currentLetterNr := floor(Nr/(26**(nrWordLength-A_Index)))
		Nr -= currentLetterNr * (26**(nrWordLength-A_Index)) 
		out .= chr(97 + currentLetterNr) 
	}
	return out
}

turnLettersIntoNumber(letters) {
	Nr := 0
	for each, letter in strSplit(letters) {
		Nr := Nr*26 + ord(letter) - 97
	}
	Return Nr 
} 
untested
Recommends AHK Studio

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Theda and 263 guests