Page 1 of 1

Newbie: Another code critique

Posted: Thu Jun 21, 2007 2:57 am
by kpeters
TIA for any suggestions of improvement for the following little function.
Kai

Code: Select all

function ProperCase pString
  local caps, tmp
  --
  put True  into caps
  repeat for each char C in pString 
    if caps then
      put toUpper( C ) after tmp
    else
      put toLower( C ) after tmp
    end if
    put ( C is in " ,-." ) into caps # stop chars go here
  end repeat
  return tmp
end ProperCase

Posted: Fri Jun 22, 2007 10:53 am
by Mark
Dear Kai,

I believe the following is what you want:

Code: Select all

function ProperCase pString,pCaps
  local tmp
  if pCaps is not true then put false into pCaps --empty?
  if pCaps then put toUpper(pString) into tmp
  else put toLower(pString) into tmp
  repeat for each char myChar in " ,-."
    replace myChar with empty in tmp
  end repeat
  return tmp
end ProperCase 
You could also use replaceText(tmp,"[, .-]",empty) instead of the repeat loop, but that is not necessarily faster and is slightly more complex.

Best,

Mark

Posted: Sun Jun 24, 2007 12:38 am
by kpeters
Hi Mark - thanks for your reply. Your code does something quite different from mine, though.

Mine intends to (as the function name suggests) propercase a string, i.e. turn something like "DR. alBERT scHWEItzer" into "Dr. Albert Schweitzer"
and I am wondering if you might have suggestions as to how to improve
my code for that purpose.

Regards,
Kai

Posted: Mon Jun 25, 2007 3:45 pm
by Klaus
Hi kai,

your code looks great to me, couldn't be scripted to run faster :-)


Best

Klaus