none
Populate a Variable with lastname of a full name text array RRS feed

  • Question

  • I have an array of full names,

    $doctors = @( 'John Q. Smith', 'Mary X. Jones', 'Thomas L. White', "Sonia M. O'Toole" )

    I would like to pass in to a variable the lastname only from that field. Or maybe only firstinitiallastname. Here is what I currently have giving me the firstnamelastinitial:

    try {
        # add firstnames to list
        $firstnames = New-Object System.Collections.ArrayList
        foreach ($doctor in $doctors) {
            $docname = ($doctor -split '\s')
            $docname = $docname[0]+$docname[-1][0]
            $firstnames += $docname

    }

    Again, I would like to see only the last name. How do I adjust this code for that?

    • Moved by Bill_Stewart Monday, July 30, 2018 9:35 PM There's no reliable algorithm for that we can give you in a forum
    Monday, June 18, 2018 7:16 PM

All replies

  • Use "-split" to get the last name of each element of the array.  THe last element is accessed as "-1"

    (-split 'John Q. Smith')[-1]


    \_(ツ)_/

    Monday, June 18, 2018 7:24 PM
  • $doctors = @(
    'John Q. Smith',
    'Mary X. Jones',
    'Thomas L. White',
    "Sonia M. O'Toole"
    )
    
    foreach ($doctor in $doctors) {
    $full = $doctor -split ' '
    $full[2]
    }


    • Edited by The Grim Monday, June 18, 2018 7:36 PM
    Monday, June 18, 2018 7:34 PM
  • $doctors = @(
    'John Q. Smith',
    'Mary X. Jones',
    'Thomas L. White',
    "Sonia M. O'Toole"
    )
    
    foreach ($doctor in $doctors) {
    $full = $doctor -split ' '
    $full[2]
    }


    Won't work for every entry in list.

    Do you see why?   Try 'John Q. Doe Jr.'


    \_(ツ)_/


    • Edited by jrv Monday, June 18, 2018 8:14 PM
    Monday, June 18, 2018 8:13 PM
  • yeah i know why; but his formal list does not contain anything past 3. 

    with that logic would your [-1] just return 'Jr.'

    Edit: also; wouldn't [2] still work as John [0] Q. [1] Doe [2] Jr. [3] ?
    • Edited by The Grim Monday, June 18, 2018 8:33 PM
    Monday, June 18, 2018 8:31 PM
  • yeah i know why; but his formal list does not contain anything past 3. 

    with that logic would your [-1] just return 'Jr.'

    Edit: also; wouldn't [2] still work as John [0] Q. [1] Doe [2] Jr. [3] ?

    Pony is … it is an example list.  You can never know what his in an arbitrary list of names.  "Names" are complex entities in a programming/data environment  and must be handled carefully.

    Take the last entry and match against a list of exclusions normal for names.  The last entries is always $array[-1].  There are lists of exclusions posted on the Internet or you can just assume a normal list and RegEx it.

    Yes.  For that specific list your code will work but doctors usually decorate their names with 'PhD', 'MD", "DDS", "FACC" and these can all be dot delimited such as "F.A.C.C.".


    \_(ツ)_/

    Monday, June 18, 2018 8:48 PM
  • : high five : 

    @gbautista72; hope this gives you a few more ways to play with your array.

    Monday, June 18, 2018 9:09 PM
  • And some people do not have middle names, so [2] is not reliable. [-1] is better, but as noted, fails for names ending in "Jr.", etc. And the last name could be "van Beethoven", or similar.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Monday, June 18, 2018 10:49 PM
  • Another common name uses a compound last name.  This is very hard to detect.

    "Mary C. Engels Smith"

    "Helen Bonham Carter"

    "Francisco Rodriguez Pizano"

    "Beyoncé Knowles Carter"

    By the millions in the US and Europe.


    \_(ツ)_/

    Monday, June 18, 2018 10:55 PM
  • Thanks I will give it a whirl.  
    Monday, June 18, 2018 11:45 PM
  • Thanks I will give it a whirl.  
    Monday, June 18, 2018 11:45 PM
  • Thanks. You are absolutely correct about the doctors and all their titles.  It is challenging to script around that!
    Monday, June 18, 2018 11:45 PM
  • Yes. Thanks for the suggestions.  Let me tinker and see what works!
    Monday, June 18, 2018 11:46 PM