Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. need to parse text. Any ideas on how to write the conversion routine?

need to parse text. Any ideas on how to write the conversion routine?

Scheduled Pinned Locked Moved Visual Basic
tutorialquestionjson
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ratman2
    wrote on last edited by
    #1

    Hey fellow coders. I'm writing a program in VB that is designed to take the question and answer file for IRC trivia bots and convert the file format over to be used with another type of trivia bot. As an example of what I need to do here is an example of text I need to parse I need to convert the line below: What band got their name from the sixties movie Barbarella?*Duran Duran To this line: Duran Duran|What band got their name from the sixties movie Barbarella? Now, this program opens a text file and copies the entire file to one string variable. That code looks like this: Public Class Form1 Inherits System.Windows.Forms.Form Dim currsep As String, newsep As String, lineoftext As String, filetext As String Private Sub mnu_file_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_file_open.Click OpenFileDialog.Filter = "Text Files (*.txt)|*.txt" OpenFileDialog.ShowDialog() If OpenFileDialog.FileName <> "" Then FileOpen(1, OpenFileDialog.FileName, OpenMode.Input) Do Until EOF(1) 'read file into RAM lineoftext = LineInput(1) filetext = filetext & lineoftext & vbCrLf Loop lbl_open.Text = "File is open and ready to be converted" FileClose(1) End If End Sub Private Sub cmd_conv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_conv.Click 'this procedure takes the file (in variable filetext) and performs the parsing. End Sub End Class any ideas for cmd_conv_click?

    D 1 Reply Last reply
    0
    • R ratman2

      Hey fellow coders. I'm writing a program in VB that is designed to take the question and answer file for IRC trivia bots and convert the file format over to be used with another type of trivia bot. As an example of what I need to do here is an example of text I need to parse I need to convert the line below: What band got their name from the sixties movie Barbarella?*Duran Duran To this line: Duran Duran|What band got their name from the sixties movie Barbarella? Now, this program opens a text file and copies the entire file to one string variable. That code looks like this: Public Class Form1 Inherits System.Windows.Forms.Form Dim currsep As String, newsep As String, lineoftext As String, filetext As String Private Sub mnu_file_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_file_open.Click OpenFileDialog.Filter = "Text Files (*.txt)|*.txt" OpenFileDialog.ShowDialog() If OpenFileDialog.FileName <> "" Then FileOpen(1, OpenFileDialog.FileName, OpenMode.Input) Do Until EOF(1) 'read file into RAM lineoftext = LineInput(1) filetext = filetext & lineoftext & vbCrLf Loop lbl_open.Text = "File is open and ready to be converted" FileClose(1) End If End Sub Private Sub cmd_conv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_conv.Click 'this procedure takes the file (in variable filetext) and performs the parsing. End Sub End Class any ideas for cmd_conv_click?

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Don't read the entire file into a string variable. That isn't necessary at all an makes you job that much harder. What you do is open 2 files. 1 is the file you want to convert, 2 is the file you want to convert to. You do the same thing you did in your sample code with the LineInput but you look for the '*' character and split the string into two strings. Then recombine the strings into the new format and write that into the second file.

      Private Sub Convert()
          Dim SourceString As String
          Dim TargetString As String
          Dim Parsed() As String
      
          FileOpen(1, "C:\\Source.txt", OpenMode.Input)
          FileOpen(2, "C:\\Target.txt", OpenMode.Output)
          Do Until EOF(1)
              SourceString = LineInput(1)
              Parsed = SourceString.Split("\*")
              TargetString = Parsed(1) & "|" & Parsed(0)
              PrintLine(2, TargetString)
          Loop
          FileClose(1, 2)
      End Sub
      

      RageInTheMachine9532

      R 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Don't read the entire file into a string variable. That isn't necessary at all an makes you job that much harder. What you do is open 2 files. 1 is the file you want to convert, 2 is the file you want to convert to. You do the same thing you did in your sample code with the LineInput but you look for the '*' character and split the string into two strings. Then recombine the strings into the new format and write that into the second file.

        Private Sub Convert()
            Dim SourceString As String
            Dim TargetString As String
            Dim Parsed() As String
        
            FileOpen(1, "C:\\Source.txt", OpenMode.Input)
            FileOpen(2, "C:\\Target.txt", OpenMode.Output)
            Do Until EOF(1)
                SourceString = LineInput(1)
                Parsed = SourceString.Split("\*")
                TargetString = Parsed(1) & "|" & Parsed(0)
                PrintLine(2, TargetString)
            Loop
            FileClose(1, 2)
        End Sub
        

        RageInTheMachine9532

        R Offline
        R Offline
        ratman2
        wrote on last edited by
        #3

        That code works.....for the most part. The problem is if the question/answer set is not all on one line it will crash with a "Index was outside the bounds of the array". error. one condition that will cause this is having a question/answer run over onto another line. Here's the current code: Private Sub mnu_file_convert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_file_convert.Click currsep = txt_current.Text newsep = txt_desired.Text OpenFileDialog.Filter = "Text Files (*.txt)|*.txt" SaveFileDialog.Filter = "Text Files (*.txt)|*.txt" OpenFileDialog.ShowDialog() SaveFileDialog.ShowDialog() If OpenFileDialog.FileName <> "" And SaveFileDialog.FileName <> "" Then FileOpen(1, OpenFileDialog.FileName, OpenMode.Input) FileOpen(2, SaveFileDialog.FileName, OpenMode.Output) 'Stop Do Until EOF(1) SourceString = LineInput(1) Parsed = SourceString.Split(currsep) If lst_current.SelectedIndex = 1 And lst_desired.SelectedIndex = 0 Then TargetString = Parsed(1) & newsep & Parsed(0) PrintLine(2, TargetString) Else TargetString = Parsed(0) & newsep & Parsed(1) PrintLine(2, TargetString) End If Loop End If End Sub End Class

        D 1 Reply Last reply
        0
        • R ratman2

          That code works.....for the most part. The problem is if the question/answer set is not all on one line it will crash with a "Index was outside the bounds of the array". error. one condition that will cause this is having a question/answer run over onto another line. Here's the current code: Private Sub mnu_file_convert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu_file_convert.Click currsep = txt_current.Text newsep = txt_desired.Text OpenFileDialog.Filter = "Text Files (*.txt)|*.txt" SaveFileDialog.Filter = "Text Files (*.txt)|*.txt" OpenFileDialog.ShowDialog() SaveFileDialog.ShowDialog() If OpenFileDialog.FileName <> "" And SaveFileDialog.FileName <> "" Then FileOpen(1, OpenFileDialog.FileName, OpenMode.Input) FileOpen(2, SaveFileDialog.FileName, OpenMode.Output) 'Stop Do Until EOF(1) SourceString = LineInput(1) Parsed = SourceString.Split(currsep) If lst_current.SelectedIndex = 1 And lst_desired.SelectedIndex = 0 Then TargetString = Parsed(1) & newsep & Parsed(0) PrintLine(2, TargetString) Else TargetString = Parsed(0) & newsep & Parsed(1) PrintLine(2, TargetString) End If Loop End If End Sub End Class

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          OK. Then when you parse the string (TargetString=...). Check to see if the array that is returned has 2 elements in it. If not, then you have to go back and read the next line and append it to the one you already have, then parse it again and it should work. Thing is, this will only work if you have 1 question and 1 answer per line or two. If the second line contains the rest of a question, then and answer, then starts another question, you'll run into problems. I know '*' comes between a question and its answer, but is there a second seperator that appears between answer followed by a question? RageInTheMachine9532

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups