Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2004
    Posts
    19

    Unanswered: Need help with a foxpro function

    Hi all, I am in need of some help writing a foxpro function, basically what I need it to do is check to see if a person's First and Last name are either all caps or all lower case and apply the proper() function to them to make it so a name comes out like John if the firstname field is populated with JOHN.* I will need to build in exceptions for the first names like "J.P." so it doesn't make it "J. p."
    Any foxpro guru's out their who would know of how to write something like this ?

  2. #2
    Join Date
    Dec 2003
    Posts
    104
    You could always use the Upper() function to test. Here is a simplified version:

    FUNCTION CheckName
    PARAMETERS cName
    IF Upper(cName) == cName
    *... name is uppercase
    cName = Proper(cName)
    ENDIF

    RETURN cName

    For your example, you could always add IF's to check for periods within the original IF test:
    IF !('.' $ cName)

    Of course, you may have the consideration of O'Brian, Soandso III, etc.
    DSummZZZ

    Even more Fox stuff at
    www.davesummers.net/foxprolinks.htm

  3. #3
    Join Date
    Dec 2005
    Posts
    1
    Quote Originally Posted by seniormint
    Hi all, I am in need of some help writing a foxpro function, basically what I need it to do is check to see if a person's First and Last name are either all caps or all lower case and apply the proper() function to them to make it so a name comes out like John if the firstname field is populated with JOHN.* I will need to build in exceptions for the first names like "J.P." so it doesn't make it "J. p."
    Any foxpro guru's out their who would know of how to write something like this ?
    Hi

    By the time you have checked for UPPER or LOWER you might as well process the name anyway so capitalisation is consistent throughout.

    This should work in any version of Visual Foxpro.

    Regards

    stuartd

    * fixproper.prg

    lparameters cName

    local nCount as integer, nPosition as integer, nOriginalLength as integer

    * Preserve string length so can restore it later

    nOriginalLength = len(cName)

    * Call VFP's Proper function. This capitalises the initial letters
    * as (mostly) required. Might as well trim.

    cName = proper(alltrim(cName))

    * Add three spaces on the end. This is one space more than the longest string searched for (MC)
    * and is one way of avoiding an error when the search string is right at the end of the supplied string without checking the length each time

    cName = cName + space(3)

    * First off, capitalise letter immediately following a hyphen
    * E.G. Double-Barrelled, Bexhill-On-Sea

    * Loop through processing each instance found.
    nCount = 1
    nPosition = at([-], cName, nCount)

    do while nPosition > 0
    * Reassemble the string with the letter immediately
    * following the hyphen capitalised
    * (No point checking if it's a letter or a number or
    * whatever, let upper() worry about that.)
    cName = left(cName, nPosition) + ;
    upper(substr(cName, nPosition + 1, 1)) + ;
    substr(cName, nPosition + 2)

    * look for next instance
    nCount = nCount + 1
    nPosition = at([-], cName, nCount)
    enddo


    * Capitalise letter following an apostrophe - except for ['s ]
    * E.G. O'Towey & St Killeen's

    nCount = 1
    nPosition = at(['], cName, nCount)
    do while nPosition > 0

    if substr(cName, nPosition + 1, 2) # "s "
    cName = left(cName, nPosition) + ;
    upper(substr(cName, nPosition + 1, 1)) + ;
    substr(cName, nPosition + 2)
    endif
    nCount = nCount + 1
    nPosition = at(['], cName, nCount)
    enddo

    * Capitalise letter following opening bracket. Useful for addresses like
    * (Flat 3) 25 My Road, One Town (Near Another), Somecity
    nCount = 1
    nPosition = at([(], cName, nCount)
    do while nPosition > 0

    cName = left(cName, nPosition) + ;
    upper(substr(cName, nPosition + 1, 1)) + ;
    substr(cName, nPosition + 2)

    nCount = nCount + 1
    nPosition = at([(], cName, nCount)
    enddo

    * Capitalise after full stops - eg St.David's, and also acronyms (A.N.C, F.B.I.)
    nCount = 1
    nPosition = at([.], cName, nCount)
    do while nPosition > 0

    cName = left(cName, nPosition) + ;
    upper(substr(cName, nPosition + 1, 1)) + ;
    substr(cName, nPosition + 2)

    nCount = nCount + 1
    nPosition = at([.], cName, nCount)
    enddo

    * Fix "Mc"s (leave "Mac"s as "Macname" as they can go either way and
    * there's no rule)
    * Because at() is case-sensitive a search for "Mc" will not find
    * words like "tomcat" which contain the string "mc"

    * Eg Mr McCartney McFaulkner Mackenzie

    nCount = 1
    nPosition = at("Mc", cName, nCount)

    do while nPosition > 0

    cName = left(cName, nPosition + 1) + ;
    upper(substr(cName, nPosition + 2, 1)) + ;
    substr(cName, nPosition + 3)

    nCount = nCount + 1
    nPosition = at("Mc", cName, nCount)
    enddo


    * padr() will correct to the length of the passed string.
    * To change the length would be improper.. :-)

    cName = padr(cName, nOriginalLength, " ")

    return cName

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •