loop text file

I need to loop a text file line by line.
if the line contains the name I am looking for, add the whole line to a list and display in a message box or list box
here is how I did it in Applescript. Can anyone help me convert to XOJO.

    set theFile to ((path to desktop folder) & "phonebook.txt") as string
set fileHandle to open for access theFile
set the_phonebook to paragraphs of (read fileHandle)
close access fileHandle

set the_person to (the clipboard)
set the_list to ""

repeat with a_paragraph in the_phonebook
	if a_paragraph contains the_person then
		set the_name to word 1 of a_paragraph & " " & word 2 of a_paragraph
		set name_count to count the_name
		set name_count to name_count + 1 -- 11
		set the_array to the_name & tab & tab & " " & characters name_count thru -1 of a_paragraph
		set the_list to the_list & return & the_array
	end if
end repeat

display dialog the_list

This would be a simple phone list text.

Ann Jones 1234
Bob Womack 2345
Jane Doe 3333
Mark Smith 1111
Shawn Brady 4545
Shawn Brown 6654

searching for Shawn would return:
Shawn Brady 4545
Shawn Brown 6654

Check the documentation for TextInputStream and InStr to start.

Also, please help us help you by using the code tags when posting code.

here is the text format:
Shawn Brady 123-4567

here is what I have put together. Each line of text goes into column 1 box.
I want Shawn Brady in column 1 box, and phone number in column 2 box.

[code] Dim f As FolderItem
Dim textInput As TextInputStream
Dim rowFromFile, oneCell As String
Dim i As Integer
f = specialfolder.desktop.child(“CBNphonebook.txt”) //from XOJO forum
If f <> Nil And f.Exists Then
Dim tab As String = ChrB(9)
textInput = TextInputStream.Open(f)
textInput.Encoding = Encodings.MacRoman // strings are MacRoman
While Not textInput.EOF
rowFromFile = textInput.ReadLine

  // Set
  If ListBox1.ColumnCount < CountFields(rowFromFile, tab) Then
    ListBox1.ColumnCount = CountFields(rowFromFile, tab)
  End If
  
  ListBox1.AddRow("")
  For i = 1 To CountFields(rowFromFile, tab)
    Listbox1.AddRow rowFromFile  'this put the whole line in column 1, I want 1st & last name in column1, number in column 2
    Listbox1.Cell(Listbox1.LastIndex,1) = "Data here goes in column 2 (1)"
  Next
Wend
textInput.Close

End If[/code]

this works but it takes every line and put 1st and last name in column 1 and phone in column 2.

There is no coding for this but I need it to compare text in a text box, and compare it to each line of text in the phonebook file and only return if there is a match.

[code] Dim f As FolderItem
Dim textInput As TextInputStream
Dim rowFromFile, oneCell As String
Dim i As Integer
f = specialfolder.desktop.child(“CBNphonebook2.txt”) //from XOJO forum
If f <> Nil And f.Exists Then
Dim tab As String = ChrB(9)
textInput = TextInputStream.Open(f)
textInput.Encoding = Encodings.MacRoman // strings are MacRoman
While Not textInput.EOF
rowFromFile = textInput.ReadLine

  // Set
  If ListBox1.ColumnCount < CountFields(rowFromFile, tab) Then
    ListBox1.ColumnCount = CountFields(rowFromFile, tab)
  End If
  
  ListBox1.AddRow("")
  For i = 1 To CountFields(rowFromFile, tab)
    For i = 1 To CountFields(rowFromFile, tab)
      oneCell = NthField(rowFromFile, tab, i)
      ListBox1.Cell(ListBox1.ListCount - 1, i - 1) = oneCell
    Next
  Next
Wend
textInput.Close

End If[/code]

CountFields and NthField are fine for limited use, but in a case like this, would be the slow option. Instead, once you read a line, convert the parts to an array like this:

dim parts() as string = rowFromFile.Split( tab )

Now you can use parts.Ubound to check the count and cycle through the array to fill your Listbox.

You can do what you want this way:

if rowFromFile.InStr( searchField.Text ) <> 0 then
  // OK to process rowFromFile
 ...

Kem I added you line of code, it kinda works, how do I remove all rows when I do a new search. It keeps adding search results.

[code] Dim c As New Clipboard
c.Text = Phonebook_TextField.Text
c.Close

Dim f As FolderItem
Dim textInput As TextInputStream
Dim rowFromFile, oneCell As String
Dim i As Integer
f = specialfolder.desktop.child(“CBNphonebook2.txt”) //from XOJO forum
If f <> Nil And f.Exists Then
Dim tab As String = ChrB(9)
textInput = TextInputStream.Open(f)
textInput.Encoding = Encodings.MacRoman // strings are MacRoman
While Not textInput.EOF
rowFromFile = textInput.ReadLine

  // Set
  If ListBox1.ColumnCount < CountFields(rowFromFile, tab) Then
    ListBox1.ColumnCount = CountFields(rowFromFile, tab)
  End If
  
  if rowFromFile.InStr( c.Text ) <> 0 then  'code from Kem
    ListBox1.AddRow("")
    For i = 1 To CountFields(rowFromFile, tab)
      For i = 1 To CountFields(rowFromFile, tab)
        oneCell = NthField(rowFromFile, tab, i)
        ListBox1.Cell(ListBox1.ListCount - 1, i - 1) = oneCell
      Next
    Next
  end 
Wend
textInput.Close

End If
[/code]

even faster

s=textInput.readAll
rows=split(s,endofline)
for i=0 to rows.ubound
parts=split(rows(i).tab)
.....

remove rows with a REDIM statement (or REMOVE if you only want to remove specific rows)

Yes, you have to DeleteAllRows between runs.

You have to DeleteAllRows or it’ll keep adding to the listbox.

I made you a demo project, this project also has an extra “keep the current selection” feature just for kicks and giggles. I think this’ll be my Week 2 project for the Just code challenge since it took me a whole 25 minutes to make. Taking RAD to a whole new level :wink:


justcode2.zip

Tim, you are one bad ass ********. Thanks so much for what you did. I tried to replace 1 line of code and it won’t work.

'dim fContacts = specialfolder.desktop.child("phonebook2.txt")

any idea how I can get your code to look at my file. I so badly want to get to where you are with XOJO.

Just select your file in the open file dialog box. I did that intentionally because using SpecialFolder.Desktop is an incredibly bad habit to get into.

However, if the file exists the swap of SpecialFolder.Desktop.Child("phonebook2.txt") and GetOpenFolderItem("") should work just fine. I wasn’t entirely certain on the format of the text file, I tried to infer from your code and it seemed like {name}{tab}{number}. The read method is incredibly simplistic and will not work with any other storage format.

It’s a great goal to work toward, just keep up with the documentation and it’s achievable. I use Xojo frequently, so I know most of the functions I need. Anything else, I look up!

Tim, here is the error I get. I have to hold Control on my Mac to show the image. Its on Dropbox.

Oh you’re forgetting to include the datatype with the dim statement! That’s what the syntax error with the first line is, and consequently it makes everything else upset -_-

Take a look at the other dim statements I wrote and compare the difference. When you see it, you’ll never forget again!