Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
JWhitney
Wanderer


Joined: 20 Oct 2006
Posts: 51

PostPosted: Mon Jul 16, 2012 8:16 pm   

Capitalizing All Words In A String (%proper?)
 
I'm trying to create a function that will capitalize all words (separated by spaces), similar to the %proper function except I want it to capitalize EACH word, not just the first.

Input:
Code:
word1 word2 word3


If @proper(@string) is used:
Code:
Word1 Word2 Word3


Any way to do this? Seems like it would be somewhat common, but I couldn't find anyone else asking for it by searching forums..
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4691
Location: Pensacola, FL, USA

PostPosted: Mon Jul 16, 2012 8:30 pm   
 
#FORALL %words(@var) {#IF (%i != %proper(%i)) {var=%replace(@var, %i, %proper(%i))}}
that should work
might end up with some odd capitalization with really short words unless you give it two passes
_________________
Discord: Shalimarwildcat
Reply with quote
JWhitney
Wanderer


Joined: 20 Oct 2006
Posts: 51

PostPosted: Tue Jul 17, 2012 1:13 am   
 
Hmm I'm trying to put this in a function, so I adjusted the @var calls to %1's like this:

Code:
#LOCAL $titleproper

#FORALL %word(%1) {#IF (%i != %proper(%i)) {$titleproper = %replace(%1, %i, %proper(%i))}}

#RETURN $titleproper


but it is only capitalizing the last item in the string, and ignoring the rest. The idea is I want to be able to leave the string itself unaltered, but when using the function like @titleproper(@stringlistname), it will display with proper casing on each word. Any way around this?
Reply with quote
JWhitney
Wanderer


Joined: 20 Oct 2006
Posts: 51

PostPosted: Tue Jul 17, 2012 1:19 am   
 
Found a way around it, like this:

Code:
#LOCAL $titleproper

$titleproper = %1

#FORALL %word($titleproper) {#IF (%i != %proper(%i)) {$titleproper = %replace($titleproper, %i, %proper(%i))}}

#RETURN $titleproper


Any way to do the same thing for plain text in a string, rather than a string list?
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4691
Location: Pensacola, FL, USA

PostPosted: Tue Jul 17, 2012 3:04 am   
 
that should work for strings because it is using the output of the %word function, and not he variable directly
_________________
Discord: Shalimarwildcat
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Tue Jul 17, 2012 5:06 am   
 
%word() is being incorrectly used here. %word() requires two parameters, the source string to work on and the word position you wish to return.

You may wish to convert %word() to %list(), using the space character as the delimiter:

Code:
#function ProperAllWords {
  //TODO: set up the exclusion list of words, leave blank to capitalize all words
  $disallowed_words = ""
  #loop 1,%numwords(%1) {
    $word = %word(%1,%i)
    #if ((%i = 1) or (%i = %numwords(%1))) {
      //always capitalize the first and last words
      $word = %proper($word)
    } {
      #if (!%ismember($word,$disallowed_words)) {
        //only capitalize if it's not a disallowed word
        $word = %proper($word)
      }
    }
    $result = %additem($word,$result)
  }
  #return %replace($result,"|"," ")
}
_________________
EDIT: I didn't like my old signature
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4691
Location: Pensacola, FL, USA

PostPosted: Tue Jul 17, 2012 3:55 pm   
 
for some reason i thought thats what %word did, my bad
_________________
Discord: Shalimarwildcat
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net