Results 1 to 18 of 18

Thread: [RESOLVED] READ part of string

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Resolved [RESOLVED] READ part of string

    I loop the txt file with the tipical Open FileName For Input As #fileNum.

    now i need to get the value between number and street, and store each value in MyvarNum (As String)


    in my case:

    4
    5
    50
    11
    20
    ...
    48/c

    ecc
    Attached Files Attached Files

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: READ part of string

    Quote Originally Posted by luca90 View Post
    I loop the txt file with the tipical Open FileName For Input As #fileNum.

    now i need to get the value between number and street, and store each value in MyvarNum (As String)


    in my case:

    4
    5
    50
    11
    20
    ...
    48/c

    ecc
    The file contents look like JSON, so you are probably better treating it as such rather than trying to parse it out manually. If you look in the codebank forum on here I am sure there are one or two JSON parsers available with examples on how to use them.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: READ part of string

    Quote Originally Posted by PlausiblyDamp View Post
    The file contents look like JSON, so you are probably better treating it as such rather than trying to parse it out manually. If you look in the codebank forum on here I am sure there are one or two JSON parsers available with examples on how to use them.
    tks. bro.

    but not found a good solution in codebank.

    Can you post a direct link to read this json by path?

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: READ part of string

    Quote Originally Posted by luca90 View Post
    tks. bro.

    but not found a good solution in codebank.

    Can you post a direct link to read this json by path?
    https://www.vbforums.com/showthread....rser-Generator is the first one I found, I don't use VB6 these days so I might not be much use beyond that link

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: READ part of string

    As this file may NOT be JSON, you can do something like this:

    Code:
    Private Sub Command1_Click()
        Dim sString As String
        Dim fileNum As Integer
        Dim i As Integer
        Dim j As Integer
        Dim sStreetNumber As String
        fileNum = FreeFile
        Open App.Path & "\Test.txt" For Input As #fileNum
        Do While Not EOF(fileNum)
            Line Input #fileNum, sString
            i = InStr(1, sString, "number")
            sStreetNumber = Mid(sString, i + 9, 6)
            j = InStr(1, sStreetNumber, """")
            sStreetNumber = Mid(sStreetNumber, 1, j - 1)
            Debug.Print sStreetNumber  'or put into a listbox or whatever
        Loop
        Close fileNum
    End Sub
    Now, if a street 'number' happens to be longer than 4 characters, you can do an Instr() search on "street" as well....like this:

    Code:
    Private Sub Command1_Click()
        Dim sString As String
        Dim fileNum As Integer
        Dim i As Integer
        Dim j As Integer
        Dim sStreetNumber As String
        fileNum = FreeFile
        Open App.Path & "\Test.txt" For Input As #fileNum
        Do While Not EOF(fileNum)
            Line Input #fileNum, sString
            i = InStr(1, sString, "number")
            sStreetNumber = Mid(sString, i + 9)
            j = InStr(1, sStreetNumber, "street")
            sStreetNumber = Mid(sStreetNumber, 1, j - 4)
            Debug.Print sStreetNumber  'or put into a listbox or whatever
        Loop
        Close fileNum
    End Sub
    Last edited by SamOscarBrown; May 23rd, 2022 at 07:48 AM.
    Sam I am (as well as Confused at times).

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: READ part of string

    Quote Originally Posted by SamOscarBrown View Post
    As this file may NOT be JSON, you can do something like this:

    Code:
    Private Sub Command1_Click()
        Dim sString As String
        Dim fileNum As Integer
        Dim i As Integer
        Dim j As Integer
        Dim sStreetNumber As String
        fileNum = FreeFile
        Open App.Path & "\Test.txt" For Input As #fileNum
        Do While Not EOF(fileNum)
            Line Input #fileNum, sString
            i = InStr(1, sString, "number")
            sStreetNumber = Mid(sString, i + 9, 6)
            j = InStr(1, sStreetNumber, """")
            sStreetNumber = Mid(sStreetNumber, 1, j - 1)
            Debug.Print sStreetNumber  'or put into a listbox or whatever
        Loop
        Close fileNum
    End Sub
    Now, if a street 'number' happens to be longer than 4 characters, you can do an Instr() search on "street" as well....like this:

    Code:
    Private Sub Command1_Click()
        Dim sString As String
        Dim fileNum As Integer
        Dim i As Integer
        Dim j As Integer
        Dim sStreetNumber As String
        fileNum = FreeFile
        Open App.Path & "\Test.txt" For Input As #fileNum
        Do While Not EOF(fileNum)
            Line Input #fileNum, sString
            i = InStr(1, sString, "number")
            sStreetNumber = Mid(sString, i + 9)
            j = InStr(1, sStreetNumber, "street")
            sStreetNumber = Mid(sStreetNumber, 1, j - 4)
            Debug.Print sStreetNumber  'or put into a listbox or whatever
        Loop
        Close fileNum
    End Sub
    GREAT!

    I can use the same techinque for street?

    for example to have:
    SP75
    Via Dieci Giornate
    Via Dante Alighieri
    ....
    ecc

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: READ part of string

    You can...but I would, instead, parse everything to an array...give me a minute or two and I'll post an example.

    Sammi
    Sam I am (as well as Confused at times).

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: READ part of string

    Quote Originally Posted by SamOscarBrown View Post
    You can...but I would, instead, parse everything to an array...give me a minute or two and I'll post an example.

    Sammi
    tks bro!

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: READ part of string

    It doesn't appear to be a legal JSON document, but instead a series of anonymous JSON objects that were jammed into one file.

    You'll probably have to parse it into separate documents to load into a JSON DOM object one by one, find a JSON parser that will generate an outer array wrapping these objects, modify an existing parser to do that, or roll your own custom parser for just this file format.

    It is hard to imagine anyone would have spewed this data as it is. Have you created this yourself out of something else, perhaps stripping off an outer JSON array?

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: READ part of string

    Well...having brain farts right now, so instead of using arrays, here's an example of how to add the street name:

    Code:
    Private Sub Command1_Click()
        Dim sString As String
        Dim fileNum As Integer
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim m As Integer
        Dim sStreetNumber As String
        Dim sStreetName As String
        fileNum = FreeFile
        Open App.Path & "\Test.txt" For Input As #fileNum
        Do While Not EOF(fileNum)
            Line Input #fileNum, sString
            
            'find street number
            i = InStr(1, sString, "number")
            sStreetNumber = Mid(sString, i + 9)
            j = InStr(1, sStreetNumber, "street")
            sStreetNumber = Mid(sStreetNumber, 1, j - 4)
            
            'find streetn name
            k = InStr(1, sString, "unit")
            sStreetName = Mid(sString, 1, k - 4)
            m = InStrRev(sStreetName, ":")
            sStreetName = Mid(sStreetName, m + 2)
    
    
            Debug.Print sStreetNumber & " " & sStreetName  'or put into a listbox or whatever
        Loop
        Close fileNum
    End Sub
    Sam I am (as well as Confused at times).

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: READ part of string

    Don't forget that this is pidgin-JSON. Not legal JSON but it probably still follows the syntax otherwise.

    As such you must deal with whitespace (blanks, LF, even CR and TAB), each object's properties can be in any order, may use escaping for string text, etc.

    Hacking together a specific parser might well work today and then when the originator makes a small change BLAM your program begins to fail.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: READ part of string

    Quote Originally Posted by SamOscarBrown View Post
    Well...having brain farts right now, so instead of using arrays, here's an example of how to add the street name:

    Code:
    Private Sub Command1_Click()
        Dim sString As String
        Dim fileNum As Integer
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim m As Integer
        Dim sStreetNumber As String
        Dim sStreetName As String
        fileNum = FreeFile
        Open App.Path & "\Test.txt" For Input As #fileNum
        Do While Not EOF(fileNum)
            Line Input #fileNum, sString
            
            'find street number
            i = InStr(1, sString, "number")
            sStreetNumber = Mid(sString, i + 9)
            j = InStr(1, sStreetNumber, "street")
            sStreetNumber = Mid(sStreetNumber, 1, j - 4)
            
            'find streetn name
            k = InStr(1, sString, "unit")
            sStreetName = Mid(sString, 1, k - 4)
            m = InStrRev(sStreetName, ":")
            sStreetName = Mid(sStreetName, m + 2)
    
    
            Debug.Print sStreetNumber & " " & sStreetName  'or put into a listbox or whatever
        Loop
        Close fileNum
    End Sub
    tks. sammi

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: [RESOLVED] READ part of string

    No prob...when you get the opportunity, look at how to use Instr() and Instrrev() functions. Also, this file can be put into a two dimensional array, separating ALL of the information as one might need---then just pull from the array as desired.
    Sam I am (as well as Confused at times).

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: READ part of string

    Quote Originally Posted by SamOscarBrown View Post
    As this file may NOT be JSON
    Oh that file is definitely JSON. It's not "well formed" or as dilettante said, legal JSON but it's JSON nonetheless. Such files really should be parsed using the proper tools. Using ad-hoc methods can break down if the format changes even slightly. Real parsers are written specifically to be intelligent enough to handle all nuances of a particular format.

    For example, look at the following:-

    Code:
    Dim s As String
    Code:
        DiM s              AS _
        String
    The VB6 compiler's parser can parse both forms of the above code. Ad-hoc parsers typically would not be able to handle the second one. It's the same with any document with strict and complicated formatting rules like JSON and XML. Ad-hoc parsing is just begging the universe for problems later down the line.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: [RESOLVED] READ part of string

    Well, as it may be....I have NO idea if that 'file' is a one-time shot or not. If not, then, yeah...probably my 'solution' is not the appropriate one. But then, who knows what or where the file came from, or if it is going to be 'downloaded' (with different information) in the future. OP usually doesn't share all pertinent information.

    Sammi
    Sam I am (as well as Confused at times).

  16. #16
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] READ part of string

    If you have control over the file's precise format then you can get away with simple parsing. Nothing wrong with that, just don't cry if the wheels come off your wagon later.

    I looked at my JsonBag class and changing it to accept multiple calls to extract such "stacked JSON" was pretty simple. I just added an Optional ByRef Offset parameter to its JSON property. Then looping through is simple:

    Code:
        Dim F As Integer
        Dim SourceData As String
        Dim Offset As Long 'Initially 0.
    
        F = FreeFile(0)
        Open "Test.txt" For Input As #F
        SourceData = Input$(LOF(F), #F)
        Close #F
        With New JsonBag
            Do
                .JSON(Offset) = SourceData
                If Offset < 0 Then Exit Do
                Debug.Print .Item("properties").Item("number")
            Loop
        End With

  17. #17
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: [RESOLVED] READ part of string

    you wouldn't wanna share your class, would ya?
    Sam I am (as well as Confused at times).

  18. #18
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] READ part of string

    Well I would need to update the documentation which I haven't done yet. That needs a little time because I made other changes between version 2.6 and this new 2.7 with that Offset parameter.

    And I need to do a lot more testing to fix any bugs the changes may have introduced.

    Then I would have to delete some of my attachments here, since I'm at my limit and the moderators sneered when I asked for an increase.

    You could always hack a version 2.6 for just this by making small changes though:

    Code:
    Public Property Get JSON(Optional ByRef Offset As Long = 0) As String
        CursorOut = 1
        SerializeItem vbNullString, Me
        JSON = Left$(TextOut, CursorOut - 1)
    
        'Clear for next call.  Do it here to reclaim space.
        TextOut = vbNullString
    End Property
    
    Public Property Let JSON(Optional ByRef Offset As Long = 0, ByRef RHS As String)
        Clear
    
        CursorIn = Offset + 1
        LengthIn = Len(RHS)
    
        SkipWhitespace RHS
        If Offset <> 0 Then 'We are processing stacked JSON documents beyond the 1st document.
            If CursorIn > LengthIn Then
                Offset = -1 'No more stacked JSON documents.
                Exit Property
            End If
        End If
    
        Select Case Mid$(RHS, CursorIn, 1)
            Case LBRACE
                CursorIn = CursorIn + 1
                mIsArray = False
                ParseObject RHS, CursorIn, LengthIn
            Case LBRACKET
                CursorIn = CursorIn + 1
                mIsArray = True
                ParseArray RHS, CursorIn, LengthIn
            Case Else
                Error99A0 "either " & LBRACE & " or " & LBRACKET, CursorIn
        End Select
        Offset = CursorIn - 1
    End Property
    I think that making those RED changes to version 2.6 are all that you would need.

    Edit:

    Already found a bug. Look for the BLUE "- 1" added near the end of those changes.

    CursorIn is a 1-based value and Offset is 0-based, so that adjustment is required there.


    Earlier versions posted at: JsonBag, Another JSON Parser/Generator
    Last edited by dilettante; May 23rd, 2022 at 03:31 PM.

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