| 12-07-2002, 05:10 AM | #1 |
Most of you will know what a parser is but for those that don't, it takes a string a pulls each word out. Here is a version that allows you to pick the word you want. Code:
//=============================================
// The Parser (Takes a String, Seperates words, Returns word you wanted)
// by DKSlayer
//=============================================
function Parser takes string ChatMsg, integer Word returns string
//Required Variables
local string array Chkstr
local integer Last = 0
local integer A = 1
local integer I = 0
local integer Length = 1
//Finds the Length of String
loop
exitwhen((SubStringBJ(ChatMsg,1,Length) == ChatMsg))
set Length = Length + 1
endloop
//Pulls Words and places them each in there own Variable
loop
if(SubStringBJ(ChatMsg,A,A) == " ") then
set Chkstr[i] = SubStringBJ(ChatMsg, (Last + 1), (A - 1))
set Chkstr[i] = Ucase(Chkstr[i])
set Last = A
set I = I + 1
elseif(A == Length) then
set Chkstr[i] = SubStringBJ(ChatMsg, (Last + 1), A)
set Chkstr[i] = Ucase(Chkstr[i])
endif
set A = A + 1
exitwhen(A>Length)
exitwhen((Word+1)==I)
endloop
//Returns wanted word
return Chkstr[Word]
endfunctionWith this it will only return one word since you can't return an array here is a loop that I used to get all the words from a string. Code:
//Puts each word into it's own Variable
loop
set ChatStr[i] = Parser(ChatMsg, i)
if (ChatStr[i] == null) then
set ChatStr[50] = "done"
endif
set i = i + 1
exitwhen(ChatStr[50] == "done")
endloopIf you have any questions please ask. Thanks DKSlayer |
| 12-07-2002, 05:17 AM | #2 |
This version you can just put in the same function and then you don't need calls, it also stores the stuff right into an array for you to use later. Code:
//Required Variables
local string array Chkstr
local integer Last = 0
local integer A = 1
local integer I = 0
local integer Length = 1
//Finds the Length of String
loop
exitwhen((SubStringBJ(ChatMsg,1,Length) == ChatMsg))
set Length = Length + 1
endloop
//Pulls Words and places them each in there own Variable
loop
if(SubStringBJ(ChatMsg,A,A) == " ") then
set Chkstr[i] = SubStringBJ(ChatMsg, (Last + 1), (A - 1))
set Chkstr[i] = Ucase(Chkstr[i])
set Last = A
set I = I + 1
elseif(A == Length) then
set Chkstr[i] = SubStringBJ(ChatMsg, (Last + 1), A)
set Chkstr[i] = Ucase(Chkstr[i])
endif
set A = A + 1
exitwhen(A>Length)
endloopAsk if you have any questions DKSlayer |
| 12-07-2002, 05:24 AM | #3 |
I ssaw the other guys Length function and thought I would also post mine. This one is basically the code from the parser but I converted it to a function for those that don't have the time. Code:
function Length takes string Msg returns integer
//Required Variable
local integer Len = 1
//Finds the Length of String
loop
exitwhen((SubStringBJ(ChatMsg,1,Len) == ChatMsg))
set Len = Len + 1
endloop
//returns the Length
return Len
endfunctionCode:
set StringLength = Length(StringMsg) I hope some of this helps someone even if it's just to learn a technique. If you use my stuff I would appreciate A thanks, but otherwise chop it up and have fun. No posting of this onto other forums without my knowledge please. IF you have ANY QUESTIONS please ask. Thank You DKSlayer |
| 01-09-2003, 02:26 AM | #4 |
Hey DK, check each byte in the string individually to find the null character, don't compare each part with the whole, we don't know how much an operation like this involves: SubStringBJ(ChatMsg,1,Len) == ChatMsg Likely, since this is a basic language, it has to go through each character and match them one by one :P Heres what i posted in "the other guys" thread PHP Code:
Anyway, cool parser, good work. FM_TertiaryEye |
| 03-07-2003, 04:48 PM | #5 |
Adding more logic than is necessary. The simplist function to find the length of a string is... Code:
function StrLen takes string s returns integer
local integer i = 1
loop
exitwhen SubStringBJ(s,i,i) == ""
set i = i + 1
endloop
return (i - 1)
endfunction |
| 03-08-2003, 05:38 PM | #6 | |
Quote:
Simple, yes, but also incorrect :P. Ill leave it to you to find out the incorrect case, but its pretty simple. Hint: If deals with that "extra logic" you were talking about. Eye |
| 03-09-2003, 01:22 AM | #7 | |
Quote:
Well blow me down. I never actually had a case where my function was sent a NULL string. Guess that's what I get for "assuming" it would work. I stand corrected. |
