Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Atom on December 28, 2002, 07:52 PM

Title: My Seperate Function
Post by: Atom on December 28, 2002, 07:52 PM
I thought that this may be useful to some of you developers out there. This is a replacement for the "Split" Function in vb. It splits by Spaces except when the spaces are inside quotes. It also ignores double spaces outside of quotes. Im using this for some custom commands but im sure some of you might find it useful for some other purpose. (It would be good for a simple protocol).

Public Function Seperate(strCode As String) As Variant
'''By Atom'''
Dim arrTemp() As Variant, strTemp As String, i As Integer, x As Integer
Dim LastChar As String, Char As String, inQuotes As Boolean
For i = 1 To Len(strCode)
    LastChar = Char
    Char = Mid(strCode, i, 1)
    If inQuotes = False Then
        If Char = " " Then
            If LastChar <> " " Then
               strTemp = strTemp & Char
            End If
        Else
        strTemp = strTemp & Char
            If Char = Chr(34) Then inQuotes = True
        End If
    Else
        strTemp = strTemp & Char
        If Char = Chr(34) Then inQuotes = False
    End If
Next i

If Left(strTemp, 1) = " " Then strTemp = Mid(strTemp, 2)
If Right(strTemp, 1) = " " Then strTemp = Left(strTemp, Len(strTemp) - 1)
inQuotes = False
ReDim Preserve arrTemp(0)
x = 0
For i = 1 To Len(strTemp)
    Char = Mid(strTemp, i, 1)
    
        If Char = " " Then
            If inQuotes = False Then
                x = x + 1
                ReDim Preserve arrTemp(x)
            Else
                arrTemp(x) = arrTemp(x) & Char
            End If
        ElseIf Char = Chr(34) Then
            If inQuotes = False Then
                inQuotes = True
            Else
                x = x + 1
                ReDim Preserve arrTemp(x)
            End If
        Else
            arrTemp(x) = arrTemp(x) & Char
        End If
Next i
Seperate = arrTemp
End Function
Title: Re: My Seperate Function
Post by: warz on December 28, 2002, 07:56 PM
Explain "as variant". Why'd you use it? What does it do?
Title: Re: My Seperate Function
Post by: Noodlez on December 28, 2002, 07:58 PM
so that it returns as an array
Title: Re: My Seperate Function
Post by: warz on December 28, 2002, 08:00 PM
Thanks, *atom*
Title: Re: My Seperate Function
Post by: Atom on December 29, 2002, 05:34 AM
;D
Title: Re: My Seperate Function
Post by: warz on December 29, 2002, 07:28 AM
Actually that's rather usless to me
Title: Re: My Seperate Function
Post by: l)ragon on December 29, 2002, 10:38 AM
atleast he replyed huh  ::)
Title: Re: My Seperate Function
Post by: haZe on December 29, 2002, 12:22 PM
when you reply, why don't you post something we actually care about, warz? :-/
Title: Re: My Seperate Function
Post by: Yoni on December 29, 2002, 01:56 PM
You misspelled Separate :(
Misspelled words in code are fatal, especially when working in programming teams. In a hypothetical situation a different programmer might try to call the nonexistent "Separate" function, get a compiler error, look at the code, see a "Seperate" function, scratch his/head several times, <repeat>.
Title: Re: My Seperate Function
Post by: Grok on December 29, 2002, 02:09 PM
What is wrong with Split()?
Title: Re: My Seperate Function
Post by: Eibro on December 29, 2002, 04:02 PM
QuoteYou misspelled Separate :(
Misspelled words in code are fatal, especially when working in programming teams. In a hypothetical situation a different programmer might try to call the nonexistent "Separate" function, get a compiler error, look at the code, see a "Seperate" function, scratch his/head several times, <repeat>.
Not to mention words like centre/center color/colour.
Which I go trying to spell the >canadian< way :(
Title: Re: My Seperate Function
Post by: Etheran on December 30, 2002, 03:35 PM
don't you mean center/centre color/colour ?
Title: Re: My Seperate Function
Post by: warz on December 30, 2002, 04:24 PM
picky? more or less asking a stupid question...
Title: Re: My Seperate Function
Post by: Grok on December 30, 2002, 04:54 PM
What is wrong with Split()?
Title: Re:My Seperate Function
Post by: Spht on April 29, 2004, 10:55 PM
So what's wrong with Split()?
Title: Re:My Seperate Function
Post by: iago on April 30, 2004, 06:55 AM
QuoteIt splits by Spaces except when the spaces are inside quotes. It also ignores double spaces outside of quotes.

I don't think split can do that?
Title: Re:My Seperate Function
Post by: Grok on April 30, 2004, 08:48 AM
You're right.
Title: Re:My Seperate Function
Post by: Eric on April 30, 2004, 10:02 AM
Quote from: iago on April 30, 2004, 06:55 AM
QuoteIt splits by Spaces except when the spaces are inside quotes. It also ignores double spaces outside of quotes.

I don't think split can do that?
Using a combination of Split() and InStr() could.
Title: Re:My Seperate Function
Post by: Adron on April 30, 2004, 10:03 AM
Quote from: LoRd[nK] on April 30, 2004, 10:02 AM
Quote from: iago on April 30, 2004, 06:55 AM
QuoteIt splits by Spaces except when the spaces are inside quotes. It also ignores double spaces outside of quotes.

I don't think split can do that?
Using a combination of Split() and InStr() could.

Would split be very useful at all? Unless you mean to find a substring that doesn't contain any "" and then split that separately..
Title: Re:My Seperate Function
Post by: Grok on April 30, 2004, 10:18 AM
RegEx could work here.