|
-
Aug 31st, 2000, 02:07 PM
#1
Thread Starter
Frenzied Member
Hi,
I have a text file that I want VB to read and count the number of occurances of a particular word and then place that number in a varibable.
How can this be done?
Thanks,
Dan
-
Aug 31st, 2000, 02:12 PM
#2
Hyperactive Member
it would be very easy if the file was a random file.
ie:
[code]
dim i as integer
dim j(1000) as string
dim k as integer
dim text as string
text = "Word"
k = 0
open (filepath) for random as #1
for i = 1 to 1000
put #1, i, j(i)
next
close #1
for i = 1 to 1000
if j(i) = text then
k = k+1
next
else
endif
[\code]
-
Aug 31st, 2000, 02:49 PM
#3
Thread Starter
Frenzied Member
Thanks,
But I'm pretty much a newbie and I don't understand completely what the code is doing..
Can you please comment it for me?
Also, what happens if there is more than 1000 occurances? Isn't there a way to just have
the code go through and count until it can't find any more of the word?
Thanks,
Dan
-
Aug 31st, 2000, 02:56 PM
#4
Thread Starter
Frenzied Member
Also,
The line:
dim j (100 0) as string
gives a syntax error in VB6..
Dan
-
Aug 31st, 2000, 03:02 PM
#5
Thread Starter
Frenzied Member
Sorry,
One more thing I think I need to clarify..
The word that I'm trying to count will not be on it's own.. Another words, it may
be embedded in a string.. For example, I want to count how many times the string "test"
occurs in the following text file:
<fasdflkgja?test
asdfkljtest
Actually, it will look a lot different, but the concept is the same..
Again, thanks for your help..
Dan
-
Aug 31st, 2000, 03:03 PM
#6
_______
<?>..try this...
Code:
'assuming it's a one field one word file
'if it is sentences this will not work.
Option Explicit
'need this in case you have We and we
'option compare will count both
Option Compare Text
Private Sub Command1_Click()
Dim intCre As Integer
Dim myArr() As String
Dim myTotal As Integer
Dim strFindMe As String
Dim myFilePathName As String
myFilePathName = "c:\My Documents\myfile.txt"
strFindMe = InputBox("enter word to search for", "Find Me")
myTotal = 0
Open myFilePathName For Input As #1
Do While Not EOF(1)
ReDim Preserve myArr(intCre)
Input #1, myArr(intCre)
intCre = intCre + 1
Loop
Close #1
For intCre = 1 To UBound(myArr)
If myArr(intCre) = strFindMe Then
myTotal = myTotal + 1
End If
Next
MsgBox myTotal & " words found matching " & strFindMe
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Aug 31st, 2000, 03:08 PM
#7
Fanatic Member
HeSaidJoe,
youre always one step ahead of me. Anyway, now that i have written it, i am posting it anyway.
Code:
Option Explicit
Private Sub Form_Load()
MsgBox countOccurances("e:\config.txt", "the")
End Sub
Private Function countOccurances(strFile As String, strWord As String) As Long
Dim lp1 As Long, lp2 As Long
Dim bTemp() As Byte
Dim strText As String
'open the file for binary
Open strFile For Binary As #1
'redim the byte array to the size of the file
ReDim bTemp(LOF(1) - 1) As Byte
'get all of the data from the file
Get #1, , bTemp
'close the file
Close #1
'get a string of the file
strText = StrConv(bTemp, vbUnicode)
'clear the byte array, saves memory
Erase bTemp
lp2 = 1
Do
'find an occurance of strWord starting from lp2
lp1 = InStr(lp2, strText, strWord)
'if a word was found
If lp1 > 0 Then
'increment the counter
countOccurances = countOccurances + 1
'set lp2 = to the postion after the word
lp2 = lp1 + Len(strWord)
End If
'loop until a word is not found
Loop Until lp1 = 0
End Function
Iain, thats with an i by the way!
-
Aug 31st, 2000, 03:21 PM
#8
_______
<?>
I wish I were that good. Yours is faster but for it's use
I don't really think it matters.
.....the more post the bettter..lots to choose from when one doesn't work.
Later.
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Aug 31st, 2000, 03:25 PM
#9
Addicted Member
This is sort of related
I notice you are a newbie with the file thing, I have a small code here that reads a Random file, with a userdefined type, this way you can keep the file small, holding only what you want, and it's easily read back in numbers of records.
Code:
'this create a user defined type, this could be your record format
Private Type Sic2File
Sic4 As Byte
latbit As Double
lngbit As Double
id As Long
End Type
'This will start to read the *.dat file I chose
Private Sub File1_DblClick()
Dim Sic2er As Sic2File 'I create a variable of my special type
Dim Looper As Long 'a variable to keep track of the listview
ListView1.ListItems.Clear 'clears out my listview
Looper = 1 'initializes the looper to start at 1st position
If Right(File1.Path, 1) = "\" Then 'checks to see if my path is the Drive, or a subdirectory
Open File1.Path & File1.FileName For Random As #1 Len = Len(Sic2er)
Else
Open File1.Path & "\" & File1.FileName For Random As #1 Len = Len(Sic2er)
End If
'the above opens the file for random, telling it that each record is the size of my special type, so for every (2+8+8+4) bytes is a record
Do Until EOF(1) 'keep looping until end of file
Get #1, , Sic2er 'grabs a record
ListView1.ListItems.Add Looper, , Sic2er.Sic4
ListView1.ListItems(Looper).ListSubItems.Add , , Sic2er.latbit
ListView1.ListItems(Looper).ListSubItems.Add , , Sic2er.lngbit
ListView1.ListItems(Looper).ListSubItems.Add , , Sic2er.id
'the above takes the data from that record and adds it to the list
Looper = Looper + 1
'increments for the next index
Loop
Close #1 'closes the file when finished.
End Sub
you might consider doing that if your data type is consitant and static size.
It also helps keep the file size only as big as you need it
for example writing all that above as a sequential text file would be alot larger, than saving a single record as 22Bytes each.
Hope this gives some insight.
-
Aug 31st, 2000, 03:30 PM
#10
_______
<?>
dbassettt74:
The concept would be different..you would be
using line input and then you would have to check inside of each line for the string unless of course the string itself was the whole line.
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|