Easy Split Function

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
VirusEcks
Posts: 9
Joined: Jun 08, 2009 20:41

Easy Split Function

Post by VirusEcks »

Sure it isn't the Fastest Code you can Find ,But Sure it's the closest thing that any one might want

it's easy to use (Extremly Easy) and have and option that most poeple neglected ( max Count to Split )

Function :
Sub Split(Text As String, Delim As String = " ", Count As Long = -1, Ret() As String)

Text : a normal string ['not a string ptr']
Delim : optional by default = " " ['Space'] can be any printable char
Count : optional by default = -1 , can be 1 for 0 split , can be -1 for split all , can be any other for Count splits
Ret() : a normal string array ['not a string ptr']

Usage :
Dim As Long MaxCount
Dim As String s() , text , Delimeter

text = "enter the text to split"
MaxCount = 2
Delimiter = " "

split text, Delimiter, MaxCount, S()

''Now S(0) = "enter"
''Now S(1) = "the text to split"


Code :

Code: Select all

Public Sub Split(Text As String, Delim As String = " ", Count As Long = -1, Ret() As String)

	Dim As Long x, p
	If Count < 1 Then
		Do 
			x = InStr(x + 1, Text, Delim)
			p += 1
		Loop Until x = 0
		Count = p - 1
	ElseIf Count = 1 Then
		ReDim Ret(Count)
		Ret(0) = Text
	Else
		Count -= 1
	End If
	Dim RetVal(Count) As Long
	x = 0
	p = 0
	Do Until p = Count
		x = InStr(x + 1,Text,Delim)
		RetVal(p) = x
		p += 1
	Loop
	ReDim Ret(Count)
	Ret(0) = Left(Text, RetVal(0) - 1 )
	p = 1
	Do Until p = Count
		Ret(p) = Mid(Text, RetVal(p - 1) + 1, RetVal(p) - RetVal(p - 1) )
		p += 1
	Loop
	Ret(Count) = Mid(Text, RetVal(Count - 1) + 1)
	
End Sub

''' ______________
''' Virus Ecks
'''
Welcome to Any one who can contribute with heavy benchmarking ..
rolliebollocks
Posts: 2655
Joined: Aug 28, 2008 10:54
Location: new york

Post by rolliebollocks »

Here is something you may find interesting. I wrote this many moons ago for my scripting engine. It's supposed to emulate LISP's CAR/CDR in one function... But it uses a UDT to do what you do with an array... It also uses string indexing instead of left/right/mid which you may find useful.

rb

Code: Select all

Type expression
    As String lhs
    As String rhs
End Type

Declare Function Splice ( instring As String ) As expression

Dim As String inexpr, myRHS, myLHS, newEXPR

inexpr = "if at first you don't succede consult three old people"
   
    myRHS = Splice (inexpr).rhs
    ? myRHS
    myRHS = Splice (Splice ( Splice (Splice (inexpr).rhs).rhs).rhs).lhs '4th Expression
    ? myRHS
    myLHS = Splice (inexpr).lhs
    ? myLHS
    Sleep
    End
   
Function Splice ( instring As String ) As expression
   
    Dim newexpr As Expression
    Dim newp    As String
   
    For i As Integer = 0 To Len(instring) - 1
       
        If instring[i] <> 32 Then 'Needs to be replaced with Tokenizer
            newp &= Chr(instring[i])
        Else
           newexpr.lhs = newp
           newexpr.rhs = Right( instring, Len(instring) - Len(newp)-1 )
           
           Return newexpr
           
        Endif
       
    Next

End Function

stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

VirusEcks, nice job ! I like how you build an index array; similar to the implementation in fbext:
http://code.google.com/p/fb-extended-li ... -split.bas

The biggest difference is that the fbext implementation gets the split count and builds the index array in one pass, rather than two (with the help of a fixed-size array of indices, forcing an implicit maximum limit -- which is not user-configurable at the moment). Its limit parameter also allows negative values, in which all but the remaining -limit splits are returned.
VirusEcks
Posts: 9
Joined: Jun 08, 2009 20:41

Post by VirusEcks »

Guess yes you're right
i never realized that function befrore

any ways .. only reason i made this because it's concerns easy more that its concern about effeciency

sure i can add more to it like -split counts or make whole thing in 1 pass
but then it will lose the term "easy"

thanks for the tips
VirusEcks
Posts: 9
Joined: Jun 08, 2009 20:41

Slight Update

Post by VirusEcks »

after some testing i found it needed a slight update ..

Code: Select all

Sub Split(Text As String, Delim As String = " ", Count As Long = -1, Ret() As String)

	Dim As Long x, p
	If Count < 1 Then
		Do 
			x = InStr(x + 1, Text, Delim)
			p += 1
		Loop Until x = 0
		Count = p - 1
	ElseIf Count = 1 Then
		ReDim Ret(Count - 1)
		Ret(0) = Text
	Else
		Count -= 1
	End If
	Dim RetVal(Count) As Long
	x = 0
	p = 0
	Do Until p = Count
		x = InStr(x + 1,Text,Delim)
		RetVal(p) = x
		p += 1
	Loop
	ReDim Ret(Count)
	Ret(0) = Left(Text, RetVal(0) - 1 )
	p = 1
	Do Until p = Count
		Ret(p) = Mid(Text, RetVal(p - 1) + 1, RetVal(p) - RetVal(p - 1) - 1 )
		p += 1
	Loop
	Ret(Count) = Mid(Text, RetVal(Count - 1) + 1)
	
End Sub
smith125
Posts: 2
Joined: Jan 19, 2011 6:52
Location: united states

Post by smith125 »

hey guys,,,
t's easy to use (Extremly Easy) and have and option that most poeple neglected ( max Count to Split ) Function : Sub Split(Text As String, ...
Post Reply