mIRC Home    About    Download    Register    News    Help

Print Thread
#227198 31/10/10 10:12 AM
Joined: Jul 2006
Posts: 242
H
HaleyJ Offline OP
Fjord artisan
OP Offline
Fjord artisan
H
Joined: Jul 2006
Posts: 242
Hi all,

I wanted a script that will take a string and capitalise the beginning of each word in the string.

i.e /begin_caps 'hello how are you' will return 'Hello How Are You'

I have now created a script, that works, but it uses a loop and feels really bulky and inefficient. I have not scripted in a whole long time, and I was hoping someone would improve on it.

I am sure some of you will come up with some really clever regexes, but could you also show some non-regex methods, so that I can understand them too.

I paste my script below.

Code:
alias begin_caps {
  var %i = 1,%line = $1-,%result
  while ($gettok(%line,%i,32)) {
    var %x = $v1 
    %result = %result $upper($left(%x,1)) $+ $right(%x,$calc($len(%x) - 1))
    inc %i 
  }
  $iif($isid,return,echo -a) %result
}


Newbie
HaleyJ #227199 31/10/10 10:37 AM
Joined: Feb 2006
Posts: 546
J
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
you've pretty much got the right idea, except you can use $mid(%x, 2) to return %x without its first character (ie. 2nd character onwards). $mid() with its extra parameter and negative index support can often be used in place of longer and more uncomfortable $left / $right combinations. in this case, $right(%x, -1) would have also sufficed (everything except the rightmost character).

careful using loops such as while ($gettok( ... )), $null is not the only thing that stops such loops. they also stop when the number 0 or the word $false is encountered.

if methodology interests you, there is another technique involving the $* identifier:

Code:
//tokenize 32 a string of words here | var %x | scid $cid set -n $eval(%x %x, 0) $!upper($left( $* , 1)) $!+ $!mid( $* , 2) | echo -a %x


to get an idea of what's happening there, see:

Code:
//tokenize 32 a string of words here | var %x | echo -a scid $cid set -n $eval(%x %x, 0) $!upper($left( $* , 1)) $!+ $!mid( $* , 2) 


$* triggers an internal loop whereby the line is initially evaluated, then each of $1, $2, $3, etc. are substituted in for $* and the resulting command performed. since it sets up /scid commands with literal text from $1- inside, this method should only be used if you're sure $1- will not contain pieces of code that should not be evaluated.

also pay attention to the use of /set -n. /set modifies local variables if ones of the same name already exist, and the -n is useful to prevent it from evaluating mathematical expressions, such as in $begin_caps(1 + 1) in your example. i'd always recommend at least using /set -n when dealing with arbitrary pieces of text.




"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
jaytea #227202 31/10/10 04:37 PM
Joined: Jul 2006
Posts: 242
H
HaleyJ Offline OP
Fjord artisan
OP Offline
Fjord artisan
H
Joined: Jul 2006
Posts: 242
Really helpful, Thank You!


Newbie

Link Copied to Clipboard