A
Alex MacCaw
Does anyone have an example of splitting a name into first and last
names? Or is just a case of doing string.split(' ')?
names? Or is just a case of doing string.split(' ')?
Does anyone have an example of splitting a name into first and last
names? Or is just a case of doing string.split(' ')?
quoth the Alex MacCaw:Does anyone have an example of splitting a name into first and last
names? Or is just a case of doing string.split(' ')?
I'd say a regexp is overkill here.
irb(main):001:0> name = "Alex MacCaw"
=> "Alex MacCaw"
irb(main):002:0> first, last = name.split
=> ["Alex", "MacCaw"]
irb(main):003:0> first
=> "Alex"
irb(main):004:0> last
=> "MacCaw"
Note that you will have to do more work to accommodate middle names and
titles, ie: Mr, Mrs, Dr etc...
And titles with spaces in them (The Honourable, His Excellency, etc...).Hi --
quoth the Alex MacCaw:Does anyone have an example of splitting a name into first and last
names? Or is just a case of doing string.split(' ')?
I'd say a regexp is overkill here.
irb(main):001:0> name = "Alex MacCaw"
=> "Alex MacCaw"
irb(main):002:0> first, last = name.split
=> ["Alex", "MacCaw"]
irb(main):003:0> first
=> "Alex"
irb(main):004:0> last
=> "MacCaw"
Note that you will have to do more work to accommodate middle names and
titles, ie: Mr, Mrs, Dr etc...
And also last names with spaces in them (von Trapp, Vaughn Williams,
etc.).
And titles with spaces in them (The Honourable, His Excellency, etc...).Hi --
quoth the Alex MacCaw:
Does anyone have an example of splitting a name into first and last
names? Or is just a case of doing string.split(' ')?
I'd say a regexp is overkill here.
irb(main):001:0> name = "Alex MacCaw"
=> "Alex MacCaw"
irb(main):002:0> first, last = name.split
=> ["Alex", "MacCaw"]
irb(main):003:0> first
=> "Alex"
irb(main):004:0> last
=> "MacCaw"
Note that you will have to do more work to accommodate middle names and
titles, ie: Mr, Mrs, Dr etc...
And also last names with spaces in them (von Trapp, Vaughn Williams,
etc.).
Can open. Worms everywhere.Michael said:And titles with spaces in them (The Honourable, His Excellency, etc...).Hi --
On Sun, 24 Jun 2007, darren kirby wrote:
quoth the Alex MacCaw:
Does anyone have an example of splitting a name into first and last
names? Or is just a case of doing string.split(' ')?
I'd say a regexp is overkill here.
irb(main):001:0> name = "Alex MacCaw"
=> "Alex MacCaw"
irb(main):002:0> first, last = name.split
=> ["Alex", "MacCaw"]
irb(main):003:0> first
=> "Alex"
irb(main):004:0> last
=> "MacCaw"
Note that you will have to do more work to accommodate middle names and
titles, ie: Mr, Mrs, Dr etc...
And also last names with spaces in them (von Trapp, Vaughn Williams,
etc.).
And international names (though the US seems to have a broad
assortment of them already)
quoth the Alex MacCaw:Does anyone have an example of splitting a name into first and last
names? Or is just a case of doing string.split(' ')?
I'd say a regexp is overkill here.
irb(main):001:0> name = "Alex MacCaw"
=> "Alex MacCaw"
irb(main):002:0> first, last = name.split
=> ["Alex", "MacCaw"]
irb(main):003:0> first
=> "Alex"
irb(main):004:0> last
=> "MacCaw"
Note that you will have to do more work to accommodate middle names and
titles, ie: Mr, Mrs, Dr etc...
-d
name = "Mr John Joe Peter Smith"
TITLES = ["Mr", "Mrs", "Ms", "Dr"]
a = name.split
last = a.pop
title = a.shift if TITLES.include? a.first
first = a.shift
middles = a
title #=> "Mr"
first #=> "John"
middles #=> ["Joe", "Peter"]
last #=> Smith"
Hi --
name = "Mr John Joe Peter Smith"
TITLES = ["Mr", "Mrs", "Ms", "Dr"]
a = name.split
last = a.pop
title = a.shift if TITLES.include? a.first
Have mercy on us Yanks and allow for a period
first = a.shift
middles = a
title #=> "Mr"
first #=> "John"
middles #=> ["Joe", "Peter"]
last #=> Smith"
However:
name = "Mr Andrew Lloyd Webber"
# etc.
title #=> "Mr"
first #=> "Andrew"
middles #=> ["Lloyd"] (wrong)
last #=> Webber" (wrong)
found is to let users control it themselves...
Hi --
name = "Mr John Joe Peter Smith"
TITLES = ["Mr", "Mrs", "Ms", "Dr"]
a = name.split
last = a.pop
title = a.shift if TITLES.include? a.first
Have mercy on us Yanks and allow for a period
first = a.shift
middles = a
title #=> "Mr"
first #=> "John"
middles #=> ["Joe", "Peter"]
last #=> Smith"
However:
name = "Mr Andrew Lloyd Webber"
# etc.
title #=> "Mr"
first #=> "Andrew"
middles #=> ["Lloyd"] (wrong)
last #=> Webber" (wrong)
name = "The Honourable Lord Andrew, the Baron Lloyd-Webber of
Sydmonton", you mean? It's hard to come up with a trickier example.
Names are just *hard* - the only reliable way of handling them that I've
found is to let users control it themselves...
Agreed. My example makes very simple assumptions that I'd imagine
apply to the vast majority of names. However, in many computer
problems there are obscure exceptions that either break the program or
break things for the user.
Alex said:Almost any assumption you make about name parsing will be wrong. For
example, take the assumption that names are composed only of letters
and letter-like symbols.
I worked at an institution that was forced to rewrite a bunch of
name-related code for a legacy system because of a "sanity" check that
was just plain wrong... and nobody realized it until Dr. O came to
work. Now they had to allow one-letter surnames, too (they'd already
allowed one-letter given or middle names, thanks to President Truman's
middle name, S).
Reminds me of an old SF story "The Man Whose Name Wouldn't Fit." IIRC
it started with a guy getting fired because his company put in a new
computer personnel data system which had one too many characters in
the field for last name to accomodate him (and it was too expensive to
fix).
I think it ended with a neo-luddite movement with a secret weapon
which dissolved the bond between the the magnetic material and the
substrate on magnetic tapes and disks.
Don't know how many here are old enough to remember when most
computers used magnetic tape. <G>
I assume you meant that his name was one character too long, not thatRick said:Reminds me of an old SF story "The Man Whose Name Wouldn't Fit." IIRC
it started with a guy getting fired because his company put in a new
computer personnel data system which had one too many characters in
the field for last name to accomodate him (and it was too expensive to
fix).
I remember carrying boxes of punched cards. One of my previous bossesI think it ended with a neo-luddite movement with a secret weapon
which dissolved the bond between the the magnetic material and the
substrate on magnetic tapes and disks.
Don't know how many here are old enough to remember when most
computers used magnetic tape. <G>
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.