HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

String Parser & Length Function

12-07-2002, 05:10 AM#1
DKSlayer
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]

endfunction

With 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")
  endloop
The i stands for which word you want to have it return. First Word is 0 and so on. This loop just stored the returned String into an array and goes to next word till the next word is null then it knows to stop.

If you have any questions please ask.
Thanks
DKSlayer
12-07-2002, 05:17 AM#2
DKSlayer
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)
   endloop
Remember the Variables need to be put at the begining of the function so cut and paste them to the top. This is for those who only plan on using the function once in their trigger, it can be used more times but it's not efficent and the function version saves code.
Ask if you have any questions
DKSlayer
12-07-2002, 05:24 AM#3
DKSlayer
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
endfunction
There you go an example of a use of it is this.
Code:
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
FM_TertiaryEye
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:
function GetLength takes string strinteger limit returns integer

    local integer strLen

    
//1-9-03: Added a null case, for when a trigger sends a null str
    
if str == "" then
       
return 0
    
endif

    
set strLen 1
    loop
        exitwhen strLen 
== limit
        
if SubStringBJ(strstrLenstrLen) == ""  then
            set strLen 
strLen 1
            exitwhen true
        
endif
        
set strLen strLen 1
    endloop
    
return strLen

endfunction 

Anyway, cool parser, good work.

FM_TertiaryEye
03-07-2003, 04:48 PM#5
rwxr-xr-x
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
FM_TertiaryEye
Quote:
Originally posted by rwxr-xr-x
Adding more logic than is necessary. The simplist function to find the length of a string is...
[/code]


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
rwxr-xr-x
Quote:
Originally posted by FM_TertiaryEye
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


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.