How to set timezone on CentOS 5.4

mv /etc/localtime /etc/localtime.bak
ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime
nano /etc/sysconfig/clock

Cheat Sheets for PHP, CSS, and RegExp

Allow remote root access to MySQL

mysql> exit

Visual Basic Function to Split CSV into an Array

I’ve always have a hard time finding examples of how to parse CSV files, and an even harder time finding examples that do it correctly, especially when they run into fields that contain escaped commas and quotes.

The following code is the product of about 2 hours of my life, but I *think* I’ve handled most (if not all) scenarios. The code aims to be standards-compliant and to the best of my knowledge is bug free.

Please see the example usage to get an idea of how versatile this code is. All comments and suggestions are welcome and greatly appreciated. Enjoy.

Public Function SplitCSV(ByVal Data As String) As String()
    Dim lngIndex As Long, lngBookmark As Long, strArray() As String, lngArrCount As Long
    Dim lngChar As Long, lngPrevChar As Long, lngTemp As Long, lngQuoteCount As Long
    lngIndex = 1    'Track progress/position of line scan
    lngBookmark = 1 'Track start of field
    'While we still have commas left to process
    While InStr(lngIndex, Data, ",", vbBinaryCompare) <> 0
        'If this is a quoted string (starts with a quote), things get much more complex
        If AscW(Mid$(Data, lngBookmark, 1)) = 34 Then
            'Loop until we find the end of the field
            For lngIndex = lngIndex To Len(Data)
                'Get the ASCII code for the current character position
                lngChar = AscW(Mid$(Data, lngIndex, 1))
                Select Case lngChar
                    Case 34: 'Quote
                        lngQuoteCount = lngQuoteCount + 1
                    Case 44: 'Comma
                        'Even quote count and quote followed by a comma means we're done here
                        'Nested if's used to emulate shirt-circuit behavior present in other
                        'programming languages.
                        If lngPrevChar = 34 Then
                            If lngQuoteCount Mod 2 = 0 Then
                                Exit For
                            End If
                        End If
                End Select
                lngPrevChar = lngChar
            Next lngIndex
        End If
        'Jump index to next comma
        lngTemp = InStr(lngIndex, Data, ",", vbBinaryCompare)
        If lngTemp <> 0 Then lngIndex = lngTemp
        'Grab contents of CSV field and store in array
        lngArrCount = lngArrCount + 1
        ReDim Preserve strArray(lngArrCount)
        strArray(lngArrCount - 1) = Mid$(Data, lngBookmark, lngIndex - lngBookmark)
        'Set bookmark for next field to current position
        lngIndex = lngIndex + 1
        lngBookmark = lngIndex
    'Store last/trailing field
    strArray(lngArrCount) = Mid$(Data, lngBookmark)
    'Return array
    SplitCSV = strArray
    'Clean up
    Erase strArray
End Function

This is the test sub that I used to verify the results:

Public Sub Test()
    Dim strArr() As String, i As Long
    strArr = SplitCSV("a,b,c,1,2,3,""a,yo1"",""a,yo2""""but"""","",z,""a,yo3""""but"",d,")
    For i = 0 To UBound(strArr) - 1
        Debug.Print "Found #" & i & ": '" & strArr(i) & "'."
    Next i
    Erase strArr
End Sub

And the output of the test sub:

Call Test
Found #0: 'a'.
Found #1: 'b'.
Found #2: 'c'.
Found #3: '1'.
Found #4: '2'.
Found #5: '3'.
Found #6: '"a,yo1"'.
Found #7: '"a,yo2""but"","'.
Found #8: 'z'.
Found #9: '"a,yo3""but"'.
Found #10: 'd'.