get element from String$

Blitz3D Forums/Blitz3D Programming/get element from String$

This function allow to get element of a string "chaine$", Just specify the Id of the element "Offset%", and the operator "Sep$"
Graphics 800,600,0,2

Text 10,10,SSP_GET_Line$("a,b,c,d",1)
Text 10,30,SSP_GET_Line$("a,b,c,d",2)
Text 10,50,SSP_GET_Line$("a,b,c,d",3)
Text 10,70,SSP_GET_Line$("a/b/c/d",4,"/")
Text 10,90,SSP_GET_Line$("a,b,c,d",5)
Text 10,110,SSP_GET_Line$("a,b,c,d,.25",5)
Text 10,130,SSP_GET_Line$("a,b,.25,d",5)
Text 10,150,SSP_GET_Line$("a,b,.25,d",3)
Text 10,170,SSP_GET_Line$("0.02|b|c|d",2,"|")
Text 10,190,SSP_GET_Line$("0.02,b,c,d",1)
Flip
WaitKey
End


Function SSP_GET_Line$(chaine$,Offset%=1,Sep$=",")
	If Offset<1 DBG_Nfo "/X\ GetLine Erreur => Offset nul " Return False
	ligne$=chaine
	LnSize%=Len (ligne)
	Local l_var$
	Local loc1%
	If offset=1
		If LnSize>0
			loc1=Instr(ligne,Sep)
			If loc1=0
				l_var$=ligne
			Else
				l_var$=Left(ligne,loc1-1)
			EndIf
			DBG_Nfo "/!\ GetLine :"+chaine+"=> "+l_var+" Offset="+offset
			Return l_var$
		Else
			DBG_Nfo "/X\ GetLine Erreur => Chaine vierge "
		EndIf
	Else
		If LnSize%>2
			; supprime le premier argument
			Local loc2%		=	1
			loc2%=Instr(ligne$,Sep$)
			If loc2%=0
				DBG_Nfo "/X\ GetLine Erreur => la chaine '"+chaine$+"' ne contien pas assez '"+offset%+"' arguments!"
				Return False
			EndIf
			Local	l_part$	=	Right(ligne$,LnSize%-loc2%)
			Local	lnPart%	=	LnSize%
			; cherche l'offset et retourne la chaine entre 'Sep'
			For i = 2 To Offset%
				Loc2%	=	Instr (l_part$,Sep$)
				If i=offset%
					If loc2% = 0
						l_var$=l_Part$
					Else
						l_var$=Left(l_Part$,loc2%-1)
					EndIf
					If Len(l_var$)=0
						DBG_Nfo "/X\ GetLine Erreur => Retourne une chaine vide"
						Return False
					Else
						DBG_Nfo "/!\ GetLine :"+chaine$+"=> "+l_var$+" Offset="+offset%
						Return l_var$
					EndIf
				EndIf
				If loc2%=0	DBG_Nfo "/X\ GetLine Erreur => Offset trop grand " Return False
				lnPart%	=	Len(l_Part$)
				l_part$	=	Right (l_part$,lnPart%-loc2%)
				lnPart%	=	Len(l_Part$)
			Next
		Else
			DBG_Nfo "/X\ GetLine erreur => nombre de caractere insuffisant ( 3 mini ! ) !"
			Return False
		EndIf
	EndIf
End Function


Function DBG_Nfo(texte$)
	DebugLog texte
End Function



similiar function I created, but slightly different useage - mine doesn't catch errors like a null index or null token, infact looking at it again now I'm not sure what would happen if the user put in a huge offset/index value that didn't exist in the input string.

; ===========================================
; == util_getToken$
; ==
; == takes a tokenised string And returns the
; == nth tokenised value. token can only be
; == 1 char long
; == If the first character is the same as the
; == token we assume that each value is surrounded
; == by a bracing set of tokens if the first char
; == is different to the token we assume each value
; == is seperated by 1 token
; == EXAMPLES
; ==			"classname" "value"
; ==			;test;;data;;stuff;
; ==			128,55,24
; ===========================================

Function util_getToken$(instring$,token$,offset)
	If Mid$(instring$,1,1)=token$ Then
		rollingOffset=1
		returnVar$=""
		For count=1 To offset
			start1=Instr(instring$,token$,rollingOffset)
			rollingOffset=start1+1
			end1=Instr(instring$,token$,rollingOffset)
			rollingOffset=end1+1
			endPos=end1-(start1+1)
			returnVar$=Mid$(instring$,start1+1,endPos)
		Next
		Return returnVar$
	Else
		endpos=0
		For count=1 To offset
			endold=endpos+1
			endpos=Instr(instring$,token$,endold)
			If count=offset Then
				Return Mid$(instring$,endold,endpos-endold)
			EndIf
		Next	
	EndIf
End Function


I tryed your function, and it seems to be faster.
Maybe anyone should use yours instead.

I use mine in order to be sure program won't MAV, cause i'm working on a "never crash" lib of single surface particule system, and i have to be sure nothing could bug it.
But we do not all need this type of function.

Thanks Rroff !

I did some testing and mines on average 7% faster, not a huge amount but if your trying to squeeze the last drops of performance out it could make a difference... however I can't guarantee mine won't MAV if used incorrectly.