Results 1 to 9 of 9

Thread: Split() [Resolved by da_silvy]

  1. #1

    Thread Starter
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845

    Split() [Resolved by da_silvy]

    Now I have your attention...


    I need a Split() function on steroids.

    I am reading an EDI file into a string and I want split the segments into a String array.

    The segement terminator is the apostophe
    eg:

    DST+980219:1510++++1200'
    PDN+A11813+1200:1200+980209:1932'
    PDN+A11767+1200:1200+980202:1802'
    PDN+A11767+120:120+980202:1801'
    PDN+MV291644+1200:1200+980213:0745'
    PDN+MV283966+1200:1200+980209:0755'
    PDN+MV272008+1200:1200+980129:1339'
    DEL+::980219:0700+2400:LIT255::4'
    DEL+::980223:2030+1200:LIT267::4'
    DEL+::980302:2030+1200:LIT280::4'
    TCO+:MCTN++1200'
    ADI+3'

    Although this example is one segment per line, that is not the case. The EDI file is one long string.

    OK, I can hear you thinking "just use Split()" well, it's not that easy because although I want to split on the apostophe, if the apostophe has a ? in front of it, it is NOT a segment terminator.


    This is what I'm using at the moment:
    (you might recognise it as being based on the "VB6 string funtions in VB5" example from the MSDN site - although I am now using VB6)

    Code:
    Public Function SplitSegments(ByVal sIn As String, SegmentTerminator As String, ReleaseIndicator As String) As Variant
        Dim sRead As String
        Dim sOut() As String
        Dim nC As Long
        
        sRead = ReadUntil(sIn, SegmentTerminator, ReleaseIndicator)
        Do
            ReDim Preserve sOut(nC)
            sOut(nC) = sRead
            
            nC = nC + 1
            sRead = ReadUntil(sIn, SegmentTerminator, ReleaseIndicator)
            DoEvents
        Loop While sRead <> ""
        
        ReDim Preserve sOut(nC)
        sOut(nC) = sIn
        SplitSegments = sOut
    End Function
    Public Function ReadUntil(ByRef sIn As String, _
          sDelim As String, esc As String, Optional bCompare As VbCompareMethod _
        = vbBinaryCompare) As String
        Dim nPos As Long
        Dim xPos As Long
        
        xPos = InStr(1, sIn, esc & sDelim, bCompare)
        nPos = InStr(1, sIn, sDelim, bCompare)
        
        
        If xPos > 0 Then
          'escaped appostrophe has been found
          If xPos < nPos Then
            nPos = InStr(xPos + 2, sIn, sDelim, bCompare)
          End If
            
        End If
        
        If nPos > 0 Then
            ReadUntil = Left(sIn, nPos - 1)
            sIn = Mid(sIn, nPos + Len(sDelim))
        End If
    
    
    End Function
    It works fine for small files but files can be up to 1.6 MB and it becomes VERY slow


    Any ideas how I can speed things up considerably?

    Thanks
    Last edited by Mark Sreeves; Nov 7th, 2001 at 06:07 AM.
    Mark
    -------------------

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Try this man:

    VB Code:
    1. mStr = "DST+980219:1510++++1200'PDN+A11813+1200:1200+980209:1932?'PDN+A11767+1200:1200+980202:1802'PDN+A11767+120:120+980202:1801'PDN+MV291644+1200:1200+980213:0745'PDN+MV283966+1200:1200+980209:0755'PDN+MV272008+1200:1200+980129:1339'DEL+::980219:0700+2400:LIT255::4'DEL+::980223:2030+1200:LIT267::4'DEL+::980302:2030+1200:LIT280::4'TCO+:MCTN++1200'ADI 3 '"
    2. mStr = Replace(mStr, "?'", "¿")
    3. splitString = Split(mStr, "'")
    4. For i = 0 To UBound(splitString)
    5.     splitString(i) = Replace(splitString(i), "¿", "?'")
    6. Next

    That is how i tested it, very fast

    Does it work for you/your needs?

  3. #3
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    That stuffed up the original string.

    I whacked a ?' in there to test it

    VB Code:
    1. mStr = "DST+980219:1510++++1200'PDN+A11813+1200:1200+980209:1932?'PDN+A11767+1200:1200+980202:1802" & _
    2.        "'PDN+A11767+120:120+980202:1801'PDN+MV291644+1200:1200+980213:0745'PDN+MV283966+1200:1200+" & _
    3.        "980209:0755'PDN+MV272008+1200:1200+980129:1339'DEL+::980219:0700+2400:LIT255::4'DEL+::9802" & _
    4.        "23:2030+1200:LIT267::4'DEL+::980302:2030+1200:LIT280::4'TCO+:MCTN++1200'ADI 3'"
    5. mStr = Replace(mStr, "?'", "¿")
    6. splitString = Split(mStr, "'")
    7. For i = 0 To UBound(splitString)
    8.     splitString(i) = Replace(splitString(i), "¿", "?'")

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Or something along these lines ;

    VB Code:
    1. Private Function superSplit(strString As String, strDelimeter As String) As String()
    2.     Dim retVal() As String, i As Long, n As Long, recording As Boolean
    3.     ReDim retVal(0)
    4.     If InStr(strString, "'") <> 0 Then retVal(0) = Mid(strString, 1, InStr(strString, "'") - 1)
    5.     For i = 2 To Len(strString)
    6.         If (Mid(strString, i, 1) = strDelimeter) And (Mid(strString, i - 1, 1) <> "?") Then
    7.             Debug.Print "ok " & recording
    8.             recording = Not recording
    9.             If recording Then
    10.                 n = i + 1
    11.             Else
    12.                 recording = False
    13.                 ReDim Preserve retVal(UBound(retVal) + 1)
    14.                 retVal(UBound(retVal)) = Mid(strString, n, i - 1)
    15.             End If
    16.         End If
    17.     Next
    18.     superSplit = retVal
    19. End Function

    Though I think da_silvy's approach is the easiest ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Thank you you irish fool

    j/k

  6. #6

    Thread Starter
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    Thanks da_silvy I read your PM, I was just testing it.

    It's VERY neat and like you say; VERY fast

    Processing time reduced from about 1½ hours down to 2 minutes!

    I'm impressed!

    Thanks


    What made you think of that method?
    Mark
    -------------------

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Fair ****s to ya. 90 minutes down to 2. Christ
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  8. #8
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Thanks very much

    Dunno what made me think of it.

    I thought that it would be easier to split it if there were no ?' things in the string. So i thought I could replace them, then split it, then replace the introduced character with ?' again

    i used ¿ as a replacement, because i don't think that would appear in the EDI. (Alt + 1, 6, 8)

    Glad I could help

    BTW: Thanks for the credit in the thread topic

  9. #9
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Originally posted by plenderj
    Fair ****s to ya. 90 minutes down to 2. Christ
    hehehhehe

    Not bad eh?

    For someone who gives up easier than a seal?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width