Flip Last and First Name then take away the comma?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Spiderkeys
Posts: 31
Joined: 31 Dec 2015, 07:24

Flip Last and First Name then take away the comma?

Post by Spiderkeys » 23 Jun 2016, 02:25

This has got me stumped even where to start.

I am copying a lot of names to clipboard and pasting them elsewhere

By those are arranged from Last Name to First

eg.
Smith, John
Williams, Bill
Jones, Mary

To save time I want the Clipboard to Reverse the two words from First to last and take away to comma so I got John Smith, Bill Williams, Mary Jones, etc.

I can remove the comma,

Code: Select all

StringReplace, clipboard, clipboard, `,,, All
I googled plenty of examples how to reverse all letters in words but I just want the 2 words to be flipped over. (And can't remember any example anywhere)

so technically, If Clipboard contains a Comma, then flip the two words, then get rid of the comma. (Or get rid of the comma first then flip, it doesn't bother me.)

GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: Flip Last and First Name then take away the comma?

Post by GEV » 23 Jun 2016, 02:54

See

Code: Select all

StringSplit

User avatar
Spiderkeys
Posts: 31
Joined: 31 Dec 2015, 07:24

Re: Flip Last and First Name then take away the comma?

Post by Spiderkeys » 23 Jun 2016, 04:03

Thanks I've now developed a dirty script for it

Code: Select all

start:
clipboard = ; Empty the clipboard
ClipWait

;TestString = Smith, Jamie
StringSplit, word_array, Clipboard, %A_Space%, .  ; Omits periods.
MsgBox, The 2nd word is %word_array2%.

Student=%word_array2% %word_array1%
StringReplace, Student, Student, `,,, All


MsgBox, Real name must be %student%

Clipboard=%Student%

goto, start
Now I can finetune it working around that code for my needs!.. :thumbup:

Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Flip Last and First Name then take away the comma?

Post by Shadowpheonix » 23 Jun 2016, 15:26

Code: Select all

Start:
Clipboard = 
ClipWait

Clipboard := RegExReplace(Clipboard, "msU)^(.*),([^,]*)$", "$2 $1")    ; Reverses names
StringReplace, Clipboard, Clipboard, .    ; Strips periods

IfInString, Clipboard, `n
	Loop, Parse, Clipboard, `n, `r
		MsgBox  Student # %A_Index% = %A_LoopField%
Else
	MsgBox Student = %Clipboard%

GoTo Start
I used this as my sample set...
Smith, Jr., John
Williams, Bill
Jones, Mary Jane

With my code, you can do each name individually or you can do an entire list at once.

User avatar
Spiderkeys
Posts: 31
Joined: 31 Dec 2015, 07:24

Re: Flip Last and First Name then take away the comma?

Post by Spiderkeys » 24 Jun 2016, 21:46

Thanks that will come in handy too, in the end, I just made it so CTRL X to copy a comma'ed name (and to keep CTRL +C just for normal operations), and CTRL V to paste as usual, but now I can work along your code for entire lists!

kireta
Posts: 8
Joined: 27 Jan 2015, 12:16

Re: Flip Last and First Name then take away the comma?

Post by kireta » 20 Oct 2021, 07:51

Hello. Newbie who needs help!

I would like to switch name order as described above

LAST, FIRST to FIRST LAST

Sometimes the string is LAST, FIRST MIDDLE

• The code below "disappears" after use. How to make persistent?

Start:
Clipboard =
ClipWait

Clipboard := RegExReplace(Clipboard, "msU)^(.*),([^,]*)$", "$2 $1") ; Reverses names
StringReplace, Clipboard, Clipboard, . ; Strips periods


• How to call this script with a hotkey, for example Ctrl+F11?

I know these questions are very basic....but I have tried to figure this out and am stumped. Any help would be much appreciated :-)

Post Reply

Return to “Ask for Help (v1)”