Name Permutation.

Get help with using AutoHotkey and its commands and hotkeys
Nickyeo
Posts: 18
Joined: 12 Feb 2019, 04:22

Name Permutation.

18 Jun 2019, 07:56

I am trying to code something that can help me to do name permutation using round robin. For example, John Smith David James.
The program should be able to return:

1) John Smith David James
2) Smith David James John
3) David James John Smith
4) James John Smith David

**Do note that the length of the name is unlimited, could go up to 10 words or more.

I know array is the key to my problem but this is not working but to my best effort. I don't know what's the logic to code against. I am hoping some kind soul could help me out here. Thanks in advance.

Code: Select all

FullName := Clipboard
StringSplit, word_array, FullName, %A_Space%, .  ; Omits periods.
msgbox, %word_array1% %word_array2% %word_array3% %word_array4% %word_array5%`n The Array is 

%word_array0%

loop, %word_array0%
{
1Index := a_index + 1
2Index := a_index + 2
3Index := a_index + 3
4Index := a_index + 4

a := word_array%a_index%
b := word_array%1index%
c := word_array%2index%
d := word_array%3index%
e := word_array%4index%


msgbox, %a% %b% %c% %d% %e% 
msgbox, %b% %c% %d% %e% %a% 
msgbox, %c% %d% %e% %a% %b% 
msgbox, %d% %e% %a% %b% %c% 
msgbox, %e% %a% %b% %c% %d% 

} 
wolf_II
Posts: 2683
Joined: 08 Feb 2015, 20:55

Re: Name Permutation.

18 Jun 2019, 09:36

Try this:

Code: Select all

FullName := "John Smith David James"
Names := StrSplit(FullName, A_Space, ".")
Loop, % Names.Count()
    MsgBox, % RoundRobin(Names, A_Index)



;-------------------------------------------------------------------------------
RoundRobin(ByRef Array, Start) { ; custom permutation
;-------------------------------------------------------------------------------
    Result := ""
    Loop, % Count := Array.Count()
        Result .= Array[Mod(Start-1 + A_Index-1, Count) + 1] . A_Space
    return RTrim(Result)
}
I hope that helps.
wolf_II
Posts: 2683
Joined: 08 Feb 2015, 20:55

Re: Name Permutation.

18 Jun 2019, 11:06

Here it is again, with helpful (?) comments:

Code: Select all

FullName := "John Smith David James"
Names := StrSplit(FullName, A_Space, ".")
Loop, % Names.Count()
    MsgBox, % RoundRobin(Names, A_Index)



;-------------------------------------------------------------------------------
RoundRobin(ByRef Array, Start) { ; custom permutation
;-------------------------------------------------------------------------------
    Result := ""
    Loop, % Count := Array.Count() {
        n := (Start-1) + (A_Index-1)        ; add two 0-based indices 
        m := Mod(n, Count)                  ; do 0-based modular arithmetic
        Result .= Array[m + 1] . A_Space    ; access 1-based Array
    }
    return RTrim(Result)
}
User avatar
sinkfaze
Posts: 609
Joined: 01 Oct 2013, 08:01

Re: Name Permutation.

18 Jun 2019, 12:22

Code: Select all

var=John Smith David James
Loop %	(n :=	StrSplit(var," ")).MaxIndex()
{
	For i, p in n
		out .=	p " "
	out :=	 SubStr(out,1,StrLen(out)-1) "`n", n.Push(n.RemoveAt(1))
}
MsgBox %	out
teadrinker
Posts: 851
Joined: 29 Mar 2015, 09:41
Contact:

Re: Name Permutation.

18 Jun 2019, 13:03

Code: Select all

var = John Smith David James

RegexReplace(var, "\s+",, count)
while RegExMatch(var . " " . var, "O)(\S+\s+)(?1){" . count . "}", m, m ? m.Pos(1) + m.Len(1) : 1)
   MsgBox, % m[0]
User avatar
FredOoo
Posts: 60
Joined: 07 May 2019, 21:58
Location: Paris

Re: Name Permutation.

18 Jun 2019, 13:40

The permutations of « ABCD » should be :

Code: Select all

ABCD
ABDC
ACBD
ACDB
ADBC
ADCB
BACD
BADC
BCAD
BCDA
BDAC
BDCA
CABD
CADB
CBAD
CBDA
CDAB
CDBA
DABC
DACB
DBAC
DBCA
DCAB
DCBA
No ?
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
teadrinker
Posts: 851
Joined: 29 Mar 2015, 09:41
Contact:

Re: Name Permutation.

18 Jun 2019, 13:51

FredOoo wrote: The permutations of « ABCD » should be
It depends on what you need. You meant ALL permutations, @Nickyeo did not all, but only those where word order is preserved, as I understood.
User avatar
FredOoo
Posts: 60
Joined: 07 May 2019, 21:58
Location: Paris

Re: Name Permutation.

18 Jun 2019, 13:56

May be he will come back and tell us.
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
User avatar
flyingDman
Posts: 205
Joined: 29 Sep 2013, 19:01
Location: Burbank, California

Re: Name Permutation.

18 Jun 2019, 14:19

Assuming that order is preserved and that we do not need n! results:

Code: Select all

var := "John Smith David James"
loop % StrSplit(var, A_Space).MaxIndex()
	msgbox % var := trim(SubStr(var,instr(var," ") + 1) " " SubStr(var,1,instr(var," ")))
Nickyeo
Posts: 18
Joined: 12 Feb 2019, 04:22

Re: Name Permutation.

18 Jun 2019, 18:59

Thanks guys for the enthusiastic reply. =) Appreciate it. I am living at Singapore (Different timezone) therefore I am unable to reply in time.

I will just need the name to permutate in the way I describe above, always move the first name to the back until the original name is formed.

One more important thing to add, I am using this at a workplace with restriction therefore I can only AutoHotkey v1.0.48.05. Command like StrSplit() can't be used.

Hope this clarifies. =)
User avatar
flyingDman
Posts: 205
Joined: 29 Sep 2013, 19:01
Location: Burbank, California

Re: Name Permutation.

18 Jun 2019, 19:03

Then you got a few options to choose from...
Nickyeo
Posts: 18
Joined: 12 Feb 2019, 04:22

Re: Name Permutation.

18 Jun 2019, 19:15

flyingDman wrote:
18 Jun 2019, 19:03
Then you got a few options to choose from...
The main problem here is my outdated version of AutoHotkey.

Many functions like StrSplit(), cannot work. It must use the old way,

Code: Select all

StringSplit, word_array, FullName, %A_Space%, .
In order to work.
teadrinker
Posts: 851
Joined: 29 Mar 2015, 09:41
Contact:

Re: Name Permutation.

18 Jun 2019, 19:42

Try this:

Code: Select all

var = John Smith David James

RegexReplace(var, "\s+", "", count)
while pos := RegExMatch(var . " " . var, "(\S+\s+)(?1){" . count . "}", m, m ? pos + StrLen(m1) : 1)
   MsgBox, % m
User avatar
flyingDman
Posts: 205
Joined: 29 Sep 2013, 19:01
Location: Burbank, California

Re: Name Permutation.

18 Jun 2019, 22:44

The main problem here is my outdated version of AutoHotkey.
Is it that difficult to update?...
wolf_II
Posts: 2683
Joined: 08 Feb 2015, 20:55

Re: Name Permutation.

19 Jun 2019, 01:28

My attempt (using modular arithmetic) written and tested with AHK-Basic v1.0.48.05:

Code: Select all

; tested with AHK-Basic v1.0.48.05

FullName := "John Smith David James"
StringSplit, Names, FullName, %A_Space%, .  ; omit periods
Loop, % Names0
    MsgBox, % RoundRobin(A_Index)
exitApp



;-------------------------------------------------------------------------------
RoundRobin(Start) { ; custom permutation
;-------------------------------------------------------------------------------
    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
}
Nickyeo
Posts: 18
Joined: 12 Feb 2019, 04:22

Re: Name Permutation.

19 Jun 2019, 01:32

wolf_II wrote:
19 Jun 2019, 01:28
My attempt using modular arithmetic written and tested with AHK-Basic v1.0.48.05:

Code: Select all

; tested with AHK-Basic v1.0.48.05

FullName := "John Smith David James"
StringSplit, Names, FullName, %A_Space%, .  ; omit periods
Loop, % Names0
    MsgBox, % RoundRobin(A_Index)
exitApp




;-------------------------------------------------------------------------------
RoundRobin(Start) { ; custom permutation
;-------------------------------------------------------------------------------
    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
}
This works perfectly !!! thanks !!!! :superhappy:
Nickyeo
Posts: 18
Joined: 12 Feb 2019, 04:22

Re: Name Permutation.

19 Jun 2019, 01:33

teadrinker wrote:
18 Jun 2019, 19:42
Try this:

Code: Select all

var = John Smith David James

RegexReplace(var, "\s+", "", count)
while pos := RegExMatch(var . " " . var, "(\S+\s+)(?1){" . count . "}", m, m ? pos + StrLen(m1) : 1)
   MsgBox, % m
This works perfectly too !!! thanks !!!! :superhappy:
Nickyeo
Posts: 18
Joined: 12 Feb 2019, 04:22

Re: Name Permutation.

19 Jun 2019, 02:27

Guys, I am back. I try this solution separately and it works. But when I try to incorporate into my script, it doesn't work anymore. I am thinking it could be due to global variable but I am not very good at it. Can you please enlighten me?

Code: Select all

;Main Name
^2::
NamePermutation()
Return

;Alternate Name
^3::
AltNamePermutation()
Return

;Name permutation
NamePermutation()
{
;Switch to chrome
WinWait, Release 6.5.1 , 
IfWinNotActive, Release 6.5.1 , , 
WinActivate, Release 6.5.1 , 
WinWaitActive, Release 6.5.1 ,
;Copy name
Send, {Home}
sleep, 50
Click, 204, 321, 3
Sleep, 10
Send, ^c
Sleep, 10
WinWait, Untitled - Notepad, 
IfWinNotActive, Untitled - Notepad, , 
WinActivate, Untitled - Notepad, 
WinWaitActive, Untitled - Notepad,
Sleep, 10
Send, {CTRLDOWN}a{CTRLUP}
Sleep, 10
Send, {CTRLDOWN}v{CTRLUP}
Sleep, 10
Send, {Backspace}{Home}^+{Right 10}
Sleep, 10
send, ^c
sleep, 10

WinWait, Untitled - Notepad, 
IfWinNotActive, Untitled - Notepad, , 
WinActivate, Untitled - Notepad, 
WinWaitActive, Untitled - Notepad,
FullName := clipboard
StringSplit, Names, FullName, %A_Space%, .  ; omit periods
Loop, % Names0
{
clipboard := % RoundRobin(A_Index)
sleep, 10
Send, ^v{Enter}
}

Return
}



;-------------------------------------------------------------------------------
RoundRobin(Start) { ; custom permutation
;-------------------------------------------------------------------------------
    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
}




;Alternate Name permutation
AltNamePermutation()
{
Send, ^c
Sleep, 10
WinWait, Untitled - Notepad, 
IfWinNotActive, Untitled - Notepad, , 
WinActivate, Untitled - Notepad, 
WinWaitActive, Untitled - Notepad,
Sleep, 10
Send, {CTRLDOWN}v{CTRLUP}
Sleep, 10
Send, {Backspace}{Home}^+{Right 10}
Sleep, 10
send, ^c
sleep, 10

WinWait, Untitled - Notepad, 
IfWinNotActive, Untitled - Notepad, , 
WinActivate, Untitled - Notepad, 
WinWaitActive, Untitled - Notepad,
FullName := clipboard
StringSplit, Names, FullName, %A_Space%, .  ; omit periods
Loop, % Names0
{
clipboard := % RoundRobin(A_Index)
sleep, 10
Send, ^v{Enter}
}

Return

}
wolf_II
Posts: 2683
Joined: 08 Feb 2015, 20:55

Re: Name Permutation.

19 Jun 2019, 03:12

I can't test your script, but I would try to add Names := "" as the first line of the script.
UNTESTED UNTESTED UNTESTED
Nickyeo
Posts: 18
Joined: 12 Feb 2019, 04:22

Re: Name Permutation.

19 Jun 2019, 03:35

wolf_II wrote:
19 Jun 2019, 03:12
I can't test your script, but I would try to add Names := "" as the first line of the script.
UNTESTED UNTESTED UNTESTED
Sorry I have gave you a bad example. I have changed it to something simpler for troubleshooting purposes.
FYI, The script can't be executed.

Code: Select all

;Main Name
^2::
NamePermutation()
Return

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

;Name permutation
NamePermutation()
{
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
}




Return to “Ask For Help”

Who is online

Users browsing this forum: Dumitas, Rohwedder and 261 guests