PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Optimized way of getting multiple items in 1 string line...-VBForums
Results 1 to 11 of 11

Thread: [RESOLVED] Optimized way of getting multiple items in 1 string line...

  1. #1

    Thread Starter
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Resolved [RESOLVED] Optimized way of getting multiple items in 1 string line...

    I wrote a function last night(several, actually, for various purposes; adding them to a listbox, combobox, etc.) that takes string with various items in it, seperated by a specified character, and gets each item 1 by one.

    I used:

    VB Code:
    1. Split(theString, theSeperator)(itemNum)

    ...in a "For" statement to get each item. The way I found the number of items was something like:

    VB Code:
    1. txtRplc = Replace$(theString, theSeperator)
    2. itemNum = ((Len(theString) - Len(txtRplc)) / Len(theSeperator))

    ...which, in short, temporarily takes out each instance of the seperating character, and subtracts the new string length from the old length to find the number of missing characters, and then divides it by the length of characters in the seperator. Which actually gives an accurate count of how many items there are(minus 1, because 4 items only need 3 seperators but the Split function is 0 based to I just made the "For" statement go 0 to ItemNum and it works).

    Anyway, I've found it works great. But in order to do a line like this:

    "12345","imgname.jpg","item description","false"

    ...the seperating character would have to be ",", including the quotes, because it can't just be a comma. The description of the item in the line may contain a comma and that would screw everything up.



    So in doing that(making the seperation character "," and running the function), I found that the function returns a quotation at the beginning of the first item, and a quotation at the end of the last. I've made the function check the first and last character to make sure it's not a quotation, but that only works for the function if the seperation phrase contains quoations every time. In other words, if I had the line:


    [hey][my name is][brendan]

    The function wouldn't work.




    Is there any way to make the function get JUST what's in between 2 characters, whether they are both the same or not, by allowing the user to specify what the seperating characters are?


    I know it's kind of long and involved, but there are some fairly intelligent and talented people here and I figured it was worth a shot!



    Thanks in advance,

    Brendan

  2. #2
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: Optimized way of getting multiple items in 1 string line...

    to get whatever among two characters? this would also look for such cases

    mini, mono

    I mean.. you cant just say that if a character is surrounded by two same characters.. it is a separator for sure.. .

    here what you are trying to achieve. i think is a separator which is more than one character... right?

    The example you gave ... "12345","imgname.jpg","item description","false"

    Here if you get quotations as first and last characters.. that's justified.. because i can see quotation before item and after false.. because they are the part of string.. and they cannot be termed as separators unless they are with another quotation with a comma in between...


    i hope i make some sense..

  3. #3

    Thread Starter
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Optimized way of getting multiple items in 1 string line...

    Quote Originally Posted by moinkhan
    to get whatever among two characters? this would also look for such cases

    mini, mono

    I mean.. you cant just say that if a character is surrounded by two same characters.. it is a separator for sure.. .

    here what you are trying to achieve. i think is a separator which is more than one character... right?

    The example you gave ... "12345","imgname.jpg","item description","false"

    Here if you get quotations as first and last characters.. that's justified.. because i can see quotation before item and after false.. because they are the part of string.. and they cannot be termed as separators unless they are with another quotation with a comma in between...


    i hope i make some sense..
    That's the dillemma. See, sometimes the item description area will have commas, or quotations within it's quotations. So the only way of distiguising the seperators between items in that string's case, is setting the seperators to "," instead of just ,

    But then, the function will not split the " in the first and last item, unless I manually add: ", to the beginning of the line and ," to the end of the line, which is kind of silly. But I just may have to do that, I think.

  4. #4
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: Optimized way of getting multiple items in 1 string line...

    So your problem statement would be..

    How to split a text which is separated by a multicharacter separator?

  5. #5

    Thread Starter
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Optimized way of getting multiple items in 1 string line...

    Quote Originally Posted by moinkhan
    So your problem statement would be..

    How to split a text which is separated by a multicharacter separator?
    Well it's not ALWAYS a multi character statement. But sometimes it is. My question is: is there a way to create a function to automatically get the items, despite the character seperator length? I already wrote a function to do it(it's at home otherwise I'd post it), and it works great, but I'm sort of weary of using it in anything intense... I was wondering if there was a more concrete way of doing it...

  6. #6
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: Optimized way of getting multiple items in 1 string line...

    Quote Originally Posted by BrendanDavis
    Well it's not ALWAYS a multi character statement. But sometimes it is. My question is: is there a way to create a function to automatically get the items, despite the character seperator length? I already wrote a function to do it(it's at home otherwise I'd post it), and it works great, but I'm sort of weary of using it in anything intense... I was wondering if there was a more concrete way of doing it...

    There must be something fixed about the separating characters... otherwise the same series of characters might come in the original string and mess up all your functionality... say.. if you assume that anything between two same characters is a separator along with those two characters... that might not be useful... for example

    mini, mono

    in both of above words.. n is surrounded by the same character.... but that should not be treated as a separator...

  7. #7

    Thread Starter
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Optimized way of getting multiple items in 1 string line...

    Quote Originally Posted by moinkhan
    There must be something fixed about the separating characters... otherwise the same series of characters might come in the original string and mess up all your functionality... say.. if you assume that anything between two same characters is a separator along with those two characters... that might not be useful... for example

    mini, mono

    in both of above words.. n is surrounded by the same character.... but that should not be treated as a separator...
    I know it :\ It's not my doing, it's this stupid database program at work. Very poorly organized, it is. However, I've come to the conclusion it's not worth trying to rack my brain to code around it. I'd rather put my time towards something useful and a tad more functional than their POS database program. They should have excluded the " character from being entered in any field, as it is use as a seperator in the application!

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Optimized way of getting multiple items in 1 string line...

    I dunno. I just code.

    Code:
    Option Explicit
    
    Public Function ArrayBySeparator(Text As String, SepStart As String, SepEnd As String, Optional SepMid As String = vbNullString) As String()
        ArrayBySeparator = Split(Mid$(Text, 1 + Len(SepStart), Len(Text) - Len(SepStart) - Len(SepEnd)), SepEnd & SepMid & SepStart)
    End Function
    Public Function CountBySeparator(Text As String, SepStart As String, SepEnd As String, Optional SepMid As String = vbNullString) As Long
        Dim strSep As String, lngA As Long, lngLen, lngCount As Long
        strSep = SepEnd & SepMid & SepStart
        lngLen = Len(strSep)
        lngA = InStr(Text, strSep)
        lngCount = 1
        Do While lngA > 1
            lngCount = lngCount + 1
            lngA = InStr(lngA + lngLen, Text, strSep)
        Loop
        CountBySeparator = lngCount
    End Function
    Private Sub Form_Load()
        Dim strTest As String, strArray() As String
        
        strTest = "[hey][my name is][brendan]"
        MsgBox strTest & ": " & CountBySeparator(strTest, "[", "]")
        
        strTest = """12345"",""imgname.jpg"",""item description"",""false"""
        MsgBox strTest & ": " & CountBySeparator(strTest, """", """", ",")
        
        strArray = ArrayBySeparator(strTest, """", """", ",")
        Debug.Print strArray(0)
        Debug.Print strArray(1)
        Debug.Print strArray(2)
        Debug.Print strArray(3)
    End Sub
    However, this of course isn't "foolproof"... depends on what you want.

  9. #9

    Thread Starter
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [RESOLVED] Optimized way of getting multiple items in 1 string line...

    I figured it out! Wasn't too tough, actually. I was just too tired last night/this morning, I guess. I just modified my "strFindBetween" function and implimented it into the "addArraysToList" function:

    VB Code:
    1. Public Function strFindBetween(strToSearch As String, strStartPoint As String, strEndPoint As String, Optional skipInstance As Integer = 0) As String
    2.  
    3.     'Quit the function if infomation provided doesn't fit requirements
    4.     If strToSearch = "" Or strStartPoint = "" Or strEndPoint = "" Then Exit Function
    5.     'If Not InStrB(strToSearch, strStartPoint) > 0 And InStrB(strToSearch, strEndPoint) > 0 Then Exit Function
    6.    
    7.         Dim trimStrLine As String
    8.         Dim strResult As String
    9.         Dim skipInst As Integer
    10.        
    11.         trimStrLine = strToSearch
    12.        
    13.             'Skip instances of first string; default doesn't skip
    14.             For skipInst = 0 To skipInstance
    15.                 If Not InStrB(trimStrLine, strStartPoint) > 0 And Not InStrB(trimStrLine, strEndPoint) > 0 Then Exit Function
    16.                 strResult = Mid$(trimStrLine, InStr(trimStrLine, strStartPoint) + Len(strStartPoint))
    17.                 'Trim the line to the end of the second specified string
    18.                 strResult = Mid$(strResult, 1, InStr(strResult, strEndPoint) - 1)
    19.                 trimStrLine = Replace$(trimStrLine, strStartPoint & strResult & strEndPoint, "", 1, 1)
    20.             Next skipInst
    21.        
    22.         'Assign the string between the two given string-points
    23.         strFindBetween = strResult
    24.        
    25. End Function

    and then in a button click event(or a sub, if I decide to make it one):
    VB Code:
    1. Dim itemInt As Integer
    2.     Dim strItm As String
    3.     Dim arrayNum As Integer
    4.    
    5.     itemInt = 5 '4 Items total
    6.    
    7.     For arrayNum = 0 To itemInt
    8.         strItm = strFindBetween(Label1.Caption, """", """", arrayNum)
    9.             If strItm <> "" Then ListBox1.AddItem strItm: itemInt = itemInt + 1
    10.     Next arrayNum


    That works if the line looks like:

    "styleID","image name","item description","true","additional info"

    or...

    "styleID""image name""item description""true""additional info"

    or even...

    [styleID][image name][item description][true][additional info]

    or..

    [styleID],[image name],[item description],[true],[additional info]

    or even more..

    [styleID],45324523[image name].,.f,dsafl[item description],mfas.;[true]..fas388383......[additional info]


    LOL, all I have to do for that is change the start and end chrs from quotations to [ and ]

  10. #10
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Optimized way of getting multiple items in 1 string line...

    Although if you're after optimal performance, you'd probably be better off switching to byte arrays and working with them, your current method has rather many weak spots performancewise (you're reallocating a string when you could still get the information you want simply by making another InStr, thus eliminating a reallocation of a string; you're also using Replace when Left$ and Right$ would be much faster solutions).

    But that's only useful info for now if you really need more speed

  11. #11

    Thread Starter
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [RESOLVED] Optimized way of getting multiple items in 1 string line...

    Quote Originally Posted by Merri
    Although if you're after optimal performance, you'd probably be better off switching to byte arrays and working with them, your current method has rather many weak spots performancewise (you're reallocating a string when you could still get the information you want simply by making another InStr, thus eliminating a reallocation of a string; you're also using Replace when Left$ and Right$ would be much faster solutions).

    But that's only useful info for now if you really need more speed
    I'm not too keen on using byte arrays, unfortunatly. That's something I am trying to get myself into more. But as for the left/right comment, you're exactly right... which is appreciated. Replace does sort of hinder performance, so I'll change that accordingly.

    As far as what it's going to be used for, in most cases it won't be for anything too lengthly so it's not that big of a deal, really. Eventually, as I learn more, I will optimize my code and functions accordingly(obviously). But as it stands, I'm more concerned with getting the job done accurately and doing so in a reasonable manner. Making it as fast as fast can be is obviously something I'm striving for long-term, but it's not an immediate priority.

    Mainly, this may only be a concern if, say, I load loading a file line-by-line that happens to be something like, say, 20,000+ lines. As it stands, I've used a less functional, slower code to load 15,000+ similar lines into 3 seperate listboxes and it has taken no more than 3 seconds to perform. This current function will reduce that even more, I'm sure, by at least a little bit. Eventually, I'd love to reduce it even more. But like I said, it's not an immediate priority. Two to three seconds is good enough to suit the purpose, being that it's not an action that is constantly taking place.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width