HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

length function (easy to use)

12-06-2002, 04:47 AM#1
Azumarril
it doesn't give the length of the _letters_ it gives some odd number. (but is still thee string length)
i'm sure it works i use it in my voting system.
used it to compare lengths, do substrings or whatever you would do with lengths =)

PHP Code:
uses strString
strlen

call ex
GetLength("hello world")

function 
Check takes nothing returns boolean
return ( SubStringBJ(udg_strStringGetForLoopIndexB(), GetForLoopIndexB()) != "/" )
endfunction

function GetLength takes string str returns integer
 set udg_strString 
= ( str "/" )
    
set bj_forLoopBIndex 1
    set bj_forLoopBIndexEnd 
30
    loop
        exitwhen bj_forLoopBIndex 
bj_forLoopBIndexEnd
        
if (  Check() ) then
            set udg_strlen 
= ( udg_strlen )
        else
            
call DoNothing(  )
        endif
        
set bj_forLoopBIndex bj_forLoopBIndex 1
    endloop
   
return udg_strlen
endfunction 
01-09-2003, 02:19 AM#2
FM_TertiaryEye
Good Attempt, Azumarril, and it works for the most part. But putting a slash on the end isnt necessary, all you need to do is check for the null string, "". That will tell you that you are at the end of the string.

Furthermore, you can use local variables by typing local vartype varname, this is a lot more helpful than requiring your users to have global variables.

If you impose a limit on the user, you shoudl include that limit as a function argument, also you should always remove the doNothing()'s because they might actually be a procedure that is called by the interpreter (akkk). And you dont need the else since you only have one branch.

Heres an updated trigger.

PHP Code:
function GetLength takes string strinteger limit returns integer

    local integer strLen

    
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, good work (it will run faster if you take the procedures out of it if the interpreter actually puts them on the invocation stack).

Nice to see that some people are taking the time to post working triggers here, thanks azumarril.

FM_TertiaryEye
01-09-2003, 02:44 AM#3
Azumarril
thanks =P
but my wc3 editor won't except local variables so eh
and if the current len pointer is 1 why subtract 1?
01-09-2003, 10:03 AM#4
FM_TertiaryEye
Even the modified editor lets you have local vars i think, what editor do you use? :abomHUH?: Just add the local vars in on top of all your other code for any function and it should work fine.

Anyway, the subtring function starts reading at 1 because substring("a", 1, 1) is a, if you throw it a zero this substring function will always return null and all your string lengths will be zero.

So thats why it starts at 1 and what happens is that as its reading, it gets set to the null character when its one past the length, for example:

a string
______^
1234567

As you can see, the function has to stop one past the length of the string, because it has to read the null character at the end.

So therefore to get the true physical length, you have to back it up one.

BTW: I just added a handle for the null string case, a player cant enter a null string but someone using a trigger can.

FM_TertiaryEye
01-09-2003, 10:28 PM#5
Azumarril
I use normal WE...
i ment
Quote:
set strLen = strLen - 1
why subtract it if it is 1 in the beggining? does it go back to the end of the text or something?
01-10-2003, 12:14 PM#6
Krakou
If you add a else could't you remove the set strLen = strLen - 1 ?

PHP Code:
function GetLength takes string strinteger limit returns integer

    local integer strLen

    
if str == "" then
       
return 0
    
endif

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

endfunction 
01-10-2003, 10:45 PM#7
Azumarril
ok good job you can use either mothods. topic closed =D