Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scanning text for specific values & inserting into arrays

Status
Not open for further replies.

Sanderval

Technical User
Oct 6, 2004
7
0
0
US
Hello,
I'm far from being proficient in VB, but am trying to learn it for the sake of future productivity. In the meantime, I have a project that requires me to create an application that can:
1) Scan a text file for specific words
2) record other text based on the location of the previously mentioned text (i.e. find the word "alarm" in one line of a text file, then record the text in the line below)
3) Format the text & save it in a text file
4) Email said document to multiple users.

In the interest of not being overwhelmed, I'm trying to focus on one aspect at a time, which leads to my questions: what is the best way to scan a text file for multiple values, put them in an array & store them for later use?

I'm thinking I create a loop that searches for a particular string, records that & related values as part of a multi dimensional array & then dump the output to a textfile later.

New (obviously) to the forums so I apologize if my request was unclear, vague, incoherent or violated any social norms that I am unaware of. If you require any information to understand my request for assistance, or if I simply am overstepping my boundaries with my request feel free to say so. Thanks
 
Rather than use an array,you could load the whole text file into a Datatable, or alternatively an ArrayList. I find Arraylists much easier than arrays, as you dont have to pre-dimension them, you simply call the add method to add a new line.

Then process each row(text line) of the table
Finally write the changed datatable to file.

All this can be achieved using StreamReader, StreamWriter

Sweep
...if it works dont mess with it
 
you may also want to check regularexpressions for searching text with many different options. quite hard if you're new to this but will definitely benefit you in the future.

 
You could read each line of the text file into a string and then use string.indexOf to test whether a word is present.

Eg.

Dim strLine As String
Dim s As New System.IO.FileStream("filename", IO.FileMode.Open)
Dim sr As New System.IO.StreamReader(s)

While sr.Peek <> -1
strLine = sr.ReadLine()
If strLine.IndexOf("wordtolookfor") <> -1 Then
'Word Found
Else
'Word Not Found
End If
End While
 
Thanks a great deal for the suggestions. I'll try & research & implement these ideas.
 
Ok, project is progressing well so far.. but i've reached an impass.
Up to this point I've been using this code:

If strLine.IndexOf("trigger text") <> -1 Then
SiteID = Mid(strLine, 1, 15)

to pull text out of the file on a by line basis & its suited my purposes well.

However.. now I have informaiton that I have to pull 1 (or sometimes more) lines below the initial text

For example, given this line of text

Outdoor Alarm

Event Type 1

Different Instance 2

For each time i see instance Outdoor Alarm, I need to capture the text 1 or more lines below. I've looked for the code to do that but I haven't been able to find it. Any suggestions?
 
you could split the string by carrage returns, that would make each line a string in an array of strings.

Code:
dim sLines() as string = split(InputString, controlchars.cr)
dim i as integer

for i = 0 to sLines.getupperbounds(0)
  if sLines(i).indexof("Outer Alarm") <> -1 then
    Line1Later = sLines(i+1)
    Line2Later = sLines(i+2)
  end if
next

----------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top