#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Couple of Questions


    I just need help on a few things. I was trying to convert the following line of code,
    Code:
    lPos = InStr(1, OriginalString, Delimiter, vbTextCompare)
    I'm not sure exactly how I can convert this because I have no clue what the Delimiter is.
    Also, in the line, lLen = len(OriginalString), how does the OriginalString get created (This line came from VB6)?
    And my last question, for now, in Python does <> work the same way as it does in VB6? For instance, if I did, if lPos <> 0:, would it work?
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,804
    Rep Power
    1569
    These aren't part of VB; where are these variables coming from? Can you post some code to give context?

    As a quick check of the Language Reference would have shown you, the 'not equal' operator can be either <> as in Basic, or != as in C and it's relatives; the latter form is preferred, however.
    Last edited by Schol-R-LEA; January 4th, 2005 at 12:46 AM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Here is all the code.
    Code:
    Public Function SplitStr(ByVal OriginalString As String, ByRef ReturnArray() As String, ByVal Delimiter As String) As Long
        'Used to split a string by delimiters, into a dynamic array, and returns
        'the ubound of the array, i use this since vb5 doesnt have the split() function
        
        Dim sItem, lArrCnt
        Dim lLen, lPos
        
        lArrCnt = 0
        lLen = Len(OriginalString)
    
        Do
            lPos = InStr(1, OriginalString, Delimiter, vbTextCompare)
            If lPos <> 0 Then
                sItem = Left$(OriginalString, lPos - 1)
                OriginalString = Mid$(OriginalString, lPos + 1)
                If sItem <> "" Then
                    lArrCnt = lArrCnt + 1
                    If lArrCnt = 1 Then
                        ReDim ReturnArray(1 To lArrCnt) As String
                    Else
                        ReDim Preserve ReturnArray(1 To lArrCnt) As String
                    End If
                    ReturnArray(lArrCnt) = sItem
                End If
            End If
        Loop While lPos <> 0
    
        If OriginalString <> "" Then
            lArrCnt = lArrCnt + 1
            ReDim Preserve ReturnArray(1 To lArrCnt) As String
            ReturnArray(lArrCnt) = OriginalString
        End If
        
        SplitStr = lArrCnt
    End Function
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    In python, all string objects own a split() method. Your function could be converted like this:
    Code:
    def SplitStr(OriginalString, Delimiter):
        # split the string and add the original string to the
        # resulting array
        array = OriginalString.split(Delimiter) + [OriginalString]
        return len(arr), arr
    
    ...
    ...
    # split a string using the SplitStr function
    lArrCnt, array = SplitStr(some_string, my_delimiter)
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Since Pythons List() type can be evaluated with the len() funtion or iterated over directly it seems kinda pointless to do this I think; why not just use the split() method and call len() on it if you need to know the numer of elements?

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by sbkwi
    In python, all string objects own a split() method. Your function could be converted like this:
    Code:
    def SplitStr(OriginalString, Delimiter):
        # split the string and add the original string to the
        # resulting array
        array = OriginalString.split(Delimiter) + [OriginalString]
        return len(arr), arr
    
    ...
    ...
    # split a string using the SplitStr function
    lArrCnt, array = SplitStr(some_string, my_delimiter)
    You might have a problem with this function since as far as I can see the arr variable isn't defined anywhere and certainly wont return what you want. Change return len(arr), arr to return len(array), array and it should work.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    Yes, you're right, I've been too hasty, it must be "array" instead of "arr".
    Using the split method shurely is the preferred method, but if you convert code from one language to another you might want to use the old functions or function names. I found that sometimes it can make the conversion a bit easier.

IMN logo majestic logo threadwatch logo seochat tools logo