[SOLVED] Function to transpose names gives errors

Get help with using AutoHotkey and its commands and hotkeys
User avatar
menteith
Posts: 51
Joined: 04 Feb 2016, 12:22

[SOLVED] Function to transpose names gives errors

03 Jun 2016, 13:26

Hi all,

I have written a function to transpose names. Let me give you an example:

1. John Doe -> Doe, John
2. Andrew J. Nozick -> Nozick, Andrew J.

This function works fine for one name. But when I have more it adds redundant spaces.

Code: Select all

string := "Russell J. Dalton, Susan E. Scarrow, Bruce E. Cain"
string := TransposeNames(string)
msgbox % string ; it gives two spaces in Scarrow,[2 spaces]Susan E. and Cain,[2 spaces]Bruce E.
; Russell J. Dalton as first entry is changed fine

singleName := "Russell J. Dalton"
singleName := RegExReplace(singleName, "^(.+?)\s+(\S+)$", "$2, $1")
msgbox % singleName ; this is ok


TransposeNames(ByRef argument)
	{
		loop, parse, argument, csv ; `,,%A_Space%
			{
				name := RegExReplace(A_LoopField, "^(.+?)\s+(\S+)$", "$2, $1")
				all := all . "`nAU  - " . name
			}
	return all
	}
What is going on?
Last edited by menteith on 03 Jun 2016, 14:13, edited 1 time in total.
An ordinary user who needs some help with developing own programs for his own use.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Function to transpose names gives errors

03 Jun 2016, 13:37

The regular expression is matching the space after the commas with (.+?). You could allow for optional spaces at the start of A_Loopfield: name := RegExReplace(A_LoopField, "^\s*(.+?)\s+(\S+)$", "$2, $1")
User avatar
AfterLemon
Posts: 83
Joined: 30 Sep 2013, 14:27
Location: Arizona, USA

Re: Function to transpose names gives errors

03 Jun 2016, 13:46

Code: Select all

"$2, $1"
is the problem

You are reordering by grabbing everything left of the last space and putting it on the right.
Except in the 2nd and 3rd names, your list has ", Susan" which includes the space to the left of the name

This line should add a LTrim to the input

Code: Select all

name := RegExReplace(LTrim(A_LoopField), "^(.+?)\s+(\S+)$", "$2, $1")
HOME: Windows 7 x64 SP1 Ultimate | AMD FX 8350 8-Core @ 4.00GHZ | 16GB Ram
WORK: Windows 7 x64 SP1 Home Premium | AMD Athlon II X4 640 @ 3.00GHz | 8GB Ram
MOBILE: Samsung Galaxy Note 3 | 2012 Asus Nexus 7 | Zune 80Gb
User avatar
menteith
Posts: 51
Joined: 04 Feb 2016, 12:22

Re: Function to transpose names gives errors

03 Jun 2016, 14:12

Thank you guys! That's it!
An ordinary user who needs some help with developing own programs for his own use.

Return to “Ask For Help”

Who is online

Users browsing this forum: CarPiotr, TAC109 and 59 guests