LastPos() bug

A forum dedicated to George scripting questions
Post Reply
User avatar
Mads Juul
Posts: 3992
Joined: 02 May 2007, 19:18

LastPos() bug

Post by Mads Juul »

There is a bug in the included LastPos() function

if the character doesnt occur in the string a error will occur
pos = LastPos("mads","j")

will create an error

by the way i think these function would be nice to hace included in the core of the language so i didn't have to include them all the time.
and the TVPX filesdon't understand includes
-mads
User avatar
Mads Juul
Posts: 3992
Joined: 02 May 2007, 19:18

Re: LastPos() bug

Post by Mads Juul »

It's because the CHAR function cant take a position below 1
this should work

Code: Select all

FUNCTION LastPos(string,car)
	pos = LEN(string)
	
	run = 1
	
	WHILE run >0
		IF pos==0
			run = 0
		ELSE
			IF (CMP(CHAR(string,pos),car) == 1)
				run = 0
			ELSE
				pos = pos-1
			END
		END
	END
	
	IF pos == 0
		pos = -1
	END
	
	RETURN pos
END
Post Reply