Difference between revisions of "$mid"

From Scriptwiki
Jump to: navigation, search
 
Line 6: Line 6:
 
  var %i = abcde
 
  var %i = abcde
 
  echo -ag $mid(%i,2,3)
 
  echo -ag $mid(%i,2,3)
This above example will echo out bcd. This is because the it grabs the text from position ''N'' which is ''2'' in this case, ''a'' being ''1'', and ''b'' being ''2''. From this spot, we now grab then next ''I'' charaters (''3'') so we grab ''b=1 c=2 d=3''
+
This above example will echo out bcd. This is because it grabs the text from position ''N'' which is ''2'' in this case, ''a'' being ''1'', and ''b'' being ''2''. From this spot, we now grab then next ''I'' charaters (''3'') so we grab ''b=1 c=2 d=3''
  
  

Revision as of 12:45, 20 December 2005

Returns I amount of charaters from the Nth charater in text.

$mid(<text>,N[,I])

I is optional, if I is ommited, it will return from posistion N onwards.

Examples

var %i = abcde
echo -ag $mid(%i,2,3)

This above example will echo out bcd. This is because it grabs the text from position N which is 2 in this case, a being 1, and b being 2. From this spot, we now grab then next I charaters (3) so we grab b=1 c=2 d=3


This is an example to loop threw every character and check it against something.

;Set the varible %text to the string we are going to use.
var %text = There is 14 brown cows, they're hyperactive and jump over the 6 lazy red fish. :-(

;Set the varible %n to 1 and %i to the lenght of %text.
var %n = 1, %i = $len(%text)
;Creates these local varibles so they don't save as global when used.
var %letters = 0, %numbers = 0, %spaces = 0, %other = 0

;Keep looping while %n is less than or equal to %i.
while (%n <= %i) {
  ;Set %letter to the charater located at %n, only grab 1 charater.
  var %letter = $mid(%text,%n,1)

  ;If %letter is a letter, number, space or other set the corrisponding varible.
  if (%letter isletter) { inc %letters }
  elseif (%letter isnum) { inc %numbers }
  elseif (%letter == $chr(32)) { inc %spaces }
  else { inc %other }

  ;Increment %n by 1, so first loop %n = 1, then %n = 2 untill %n is greater than %i
  inc %n
}
echo -ag String: %text
echo -ag There is %letters letters, %numbers numbers, %spaces spaces and %other other charaters (punctuations etc.)

Also See