Last Name First with middle names

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
DataLife
Posts: 445
Joined: 29 Sep 2013, 19:52

Last Name First with middle names

Post by DataLife » 16 Apr 2021, 23:44

I was able to come up with a solution but I suspect there is a much shorter and smarter way to accomplish this. Maybe regex? Which I have tried and failed to understand beyond some basic regex.
Start with names like this...
Mary Jo Louise Smith
Mary Jo Smith
Mary Smith

and end up with names like this... Last Name First Name Middle Names
Smith Mary Jo Louise
Smith Mary Jo
Smith Mary

My Solution

Code: Select all

Name = Mary Jo Louise Smith
;Name = Mary Jo Smith
;Name = Mary Smith

StringSplit,OutPutArray,Name,%a_space%
LastName := OutPutArray%OutPutArray0% 
OutPutArray0--
Loop %OutPutArray0%
 TheRest := ( TheRest a_space OutPutArray%a_index% )
LastNameFirst := (LastName TheRest)
Loop % OutPutArray0+1 ;clear Array variables
 OutPutArray%a_index% =
 
 MsgBox % LastNameFirst
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
boiler
Posts: 16770
Joined: 21 Dec 2014, 02:44

Re: Last Name First with middle names

Post by boiler » 17 Apr 2021, 01:02

Code: Select all

Name = Mary Jo Louise Smith
;Name = Mary Jo Smith
;Name = Mary Smith

LastNameFirst := RegExReplace(Name, "(.*) (\S+)", "$2 $1")
 
MsgBox % LastNameFirst
You still will have problems with a last name like Van Slyke no matter what you do.
Last edited by boiler on 17 Apr 2021, 02:42, edited 2 times in total.
User avatar
boiler
Posts: 16770
Joined: 21 Dec 2014, 02:44

Re: Last Name First with middle names

Post by boiler » 17 Apr 2021, 02:09

Another non-RegEx approach:

Code: Select all

Name = Mary Jo Louise Smith
;Name = Mary Jo Smith
;Name = Mary Smith

LastNameFirst := SubStr(Name,1-(p:=StrLen(s:=StrReplace(Name," ",,,StrLen(Name)-StrLen(StrReplace(Name," "))-1))-InStr(s," "))) " " SubStr(Name,1,StrLen(Name)-p-1)

MsgBox % LastNameFirst
User avatar
boiler
Posts: 16770
Joined: 21 Dec 2014, 02:44

Re: Last Name First with middle names

Post by boiler » 17 Apr 2021, 02:29

A bit shorter version of yours:

Code: Select all

Name = Mary Jo Louise Smith
;Name = Mary Jo Smith
;Name = Mary Smith

OutputArray := StrSplit(Name, " ")
LastNameFirst := OutputArray.Pop()
loop, % OutputArray.MaxIndex() ; array is empty after loop
 LastNameFirst .= " " OutputArray.RemoveAt(1)
 
MsgBox % LastNameFirst
User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Last Name First with middle names

Post by flyingDman » 17 Apr 2021, 13:59

Another non-regex one:

Code: Select all

Name = Mary Jo Louise Smith
msgbox % substr(name,InStr(name," ",,,x:=strsplit(name," ").maxindex()-1)+1) " " substr(name,1,InStr(name," ",,,x))
14.3 & 1.3.7
User avatar
DataLife
Posts: 445
Joined: 29 Sep 2013, 19:52

Re: Last Name First with middle names

Post by DataLife » 18 Apr 2021, 00:30

@boiler
@flyingDman

thanks to both of you. All your examples suits my needs.

As far as still having issues with last names like Van Halen, the user of my script will be entering the last name in an edit box. In the Ini file I will store the last name as Van*Halen then whenever I need to display the name just remove the * (asterisk) character.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
Post Reply

Return to “Ask for Help (v1)”