Difference between revisions of "$mid"
From Scriptwiki
Line 1: | Line 1: | ||
Returns '''I''' amount of charaters from the '''N'''th charater in '''text'''. | Returns '''I''' amount of charaters from the '''N'''th charater in '''text'''. | ||
$mid(<text>,N[,I]) | $mid(<text>,N[,I]) | ||
− | I is optional, if I is ommited, it will return from | + | I is optional, if I is ommited, it will return from position N onwards. |
== Examples == | == Examples == | ||
Line 10: | Line 10: | ||
This is an example to loop threw every character and check it against something. | This is an example to loop threw every character and check it against something. | ||
− | '';Set the | + | '';Set the variable %text to the string we are going to use.'' |
− | [[var]] %text = There | + | [[var]] %text = There are 14 brown cows, they're hyperactive and jump over the 6 lazy red fish. :-( |
− | '';Set the | + | '';Set the variable %n to 1 and %i to the length of %text.'' |
var %n = 1, %i = [[$len]](%text) | var %n = 1, %i = [[$len]](%text) | ||
− | '';Creates these local | + | '';Creates these local variables so they don't save as global when used.'' |
var %letters = 0, %numbers = 0, %spaces = 0, %other = 0 | var %letters = 0, %numbers = 0, %spaces = 0, %other = 0 | ||
Line 23: | Line 23: | ||
var %letter = $mid(%text,%n,1) | var %letter = $mid(%text,%n,1) | ||
− | '';If %letter is a letter, number, space or other set the | + | '';If %letter is a letter, number, space or other set the corresponding variable.'' |
[[If-Then-Else|if]] (%letter [[If-Then-Else#The_Operators|isletter]]) { [[inc]] %letters } | [[If-Then-Else|if]] (%letter [[If-Then-Else#The_Operators|isletter]]) { [[inc]] %letters } | ||
[[If-Then-Else|elseif]] (%letter [[If-Then-Else#The_Operators|isnum]]) { inc %numbers } | [[If-Then-Else|elseif]] (%letter [[If-Then-Else#The_Operators|isnum]]) { inc %numbers } | ||
Line 29: | Line 29: | ||
[[If-Then-Else|else]] { inc %other } | [[If-Then-Else|else]] { inc %other } | ||
− | '';Increment %n by 1, so first loop %n = 1, then %n = 2 | + | '';Increment %n by 1, so first loop %n = 1, then %n = 2 until %n is greater than %i'' |
inc %n | inc %n | ||
} | } | ||
[[echo]] -ag String: %text | [[echo]] -ag String: %text | ||
− | echo -ag There | + | echo -ag There are %letters letters, %numbers numbers, %spaces spaces and %other other charaters (punctuations etc.) |
== Also See == | == Also See == | ||
* [[$right]] | * [[$right]] | ||
* [[$left]] | * [[$left]] |
Revision as of 12:48, 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 position 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 variable %text to the string we are going to use. var %text = There are 14 brown cows, they're hyperactive and jump over the 6 lazy red fish. :-( ;Set the variable %n to 1 and %i to the length of %text. var %n = 1, %i = $len(%text) ;Creates these local variables 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 corresponding variable. 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 until %n is greater than %i inc %n } echo -ag String: %text echo -ag There are %letters letters, %numbers numbers, %spaces spaces and %other other charaters (punctuations etc.)