Page 2 of 2

### Re: Name Permutation.

Posted: 19 Jun 2019, 04:16
I'm stick on permutationsâ€¦
Permutation class for demonstrating usage of enumerator
https://www.autohotkey.com/boards/viewtopic.php?style=7&f=7&t=65535

### Re: Name Permutation.

Posted: 19 Jun 2019, 04:47
@Nickyeo:

This was tested !!!
the trick is to use global Names0 inside the function. (see Help)

Code: Select all

``````; tested with AHK-Basic v1.0.48.05

^2::
NamePermutation()
Return

;Assume there is other functions
^3::
Send, FaceRecognition
Return

;Name permutation
NamePermutation()
{
global Names0
FullName := "John Smith David James"
StringSplit, Names, FullName, %A_Space%, .  ; omit periods
Loop, % Names0
{
MsgBox, % RoundRobin(A_Index)
}

}

;-------------------------------------------------------------------------------
RoundRobin(Start) {
;-------------------------------------------------------------------------------
Zero := 0, Result := ""
Loop, % Count := Names%Zero%
{
n := (Start-1) + (A_Index-1)
m := Mod(n, Count) + 1
Result .= Names%m% . A_Space
}
return Result
}
``````

### Re: Name Permutation.

Posted: 19 Jun 2019, 06:26
This example is using a label using the global namespace to avoid confusion. I tested with AHK 1.0.48.05.

Code: Select all

``````;Main Name
^2::
FullName := "John Smith David James Captain Cook"
Gosub, NamePermutation
Return

;Assume there is other functions
^3::
Send, FaceRecognition
Return

;Name permutation
NamePermutation:
StringSplit, NameParts, FullName, %A_Space%, .
Shifts := NameParts0 - 1
Loop, %NameParts0%
{
RoundRobin := ""
Loop, %NameParts0%
RoundRobin .= (A_Index > 1 ? " " : "") . NameParts%A_Index%
MsgBox, 0, # %A_Index%, %RoundRobin%
I := 1
FirstPart := NameParts%I%
Loop, %Shifts% {
J := I + 1
NameParts%I% := NameParts%J%
I += 1
}
NameParts%J% := FirstPart
}
Return``````

### Just insert at the end the item you removed at the beginning

Posted: 19 Jun 2019, 08:58

Code: Select all

``````#SingleInstance force
#Persistent

Names := ["John", "Smith", "David", "James"]

loop % lenNames := Names.length(){
print(flat( Names ))
Names.insertAt( lenNames, Names.removeAt(1) )  ; Just insert at the end the item you removed at the beginning.
}

;==========================================================

flat( liste, sep:=", " ){
; Transform array to a flat line
for index in liste
txt .= liste[index] sep
return rTrim( txt, sep)
}

print( text ){
; Who wants too many msgBox ?
static stdOut := 0
if !stdOut {
dllCall("AllocConsole")
stdOut := fileOpen("*", "w `n")
}
stdOut.writeLine( text )
stdOut.read(0) ;flush
}
``````

Code: Select all

``````John, Smith, David, James
Smith, David, James, John
David, James, John, Smith
James, John, Smith, David
``````