Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 Counting letter, how to count letter from input text file

views
     
TSdeehanie
post Mar 3 2006, 10:03 PM, updated 20y ago

New Member
*
Junior Member
14 posts

Joined: Jan 2006
Basically, i have done for counting letter(how many times 'a' occured) from string in textbox.But how if i want to count letter such that from a text file? I've been using vb for this:The code below just used to count characters, not a single letter..Can someone show me how?thanks in advance!


Dim FNum As Integer
Dim FBuf() As Byte
Dim CharCount(255) As Long
Dim LoopChar As Long

Const FileName As String = "X:\Path\File.xyz"

FNum = FreeFile() ' Open file and read contents
Open FileName For Binary Access Read Lock Write As #FNum
ReDim FBuf(LOF(FNum) - 1) As Byte
Get #FNum, , FBuf()
Close #FNum

For LoopChar = 0 To UBound(FBuf()) ' Create character counts
CharCount(FBuf(LoopChar)) = CharCount(FBuf(LoopChar)) + 1
Next LoopChar



This post has been edited by deehanie: Mar 3 2006, 10:08 PM
asc2
post Mar 3 2006, 10:39 PM

Casual
***
Junior Member
315 posts

Joined: Jan 2003
count a - z the times they occur in a text file or count how many words in a text file

This post has been edited by asc2: Mar 3 2006, 10:39 PM
silverhawk
post Mar 4 2006, 01:14 AM

Eyes on Target
Group Icon
Elite
4,956 posts

Joined: Jan 2003


Read from the file and put the contents into a string variable and read it the same way you would from a textbox.

wKkaY
post Mar 4 2006, 07:13 AM

misutā supākoru
Group Icon
VIP
6,008 posts

Joined: Jan 2003
QUOTE
ReDim FBuf(LOF(FNum) - 1) As Byte
Get #FNum, , FBuf()
This is bad practice. You will end up reading the whole file into memory - which poses a problem if the system doesn't have enough RAM available.

Read and process chunks of data at a time instead.

This post has been edited by wKkaY: Mar 4 2006, 07:13 AM
anthony_yio
post Mar 4 2006, 10:59 AM

........
Group Icon
Elite
1,828 posts

Joined: Jan 2003


QUOTE(wKkaY @ Mar 4 2006, 07:13 AM)
This is bad practice. You will end up reading the whole file into memory - which poses a problem if the system doesn't have enough RAM available.

Read and process chunks of data at a time instead.
*
Actually, there is no worry for modern day OS, system will proceed to virtual memory. But that is only for counting number of char, lol.


asc2
post Mar 4 2006, 11:27 AM

Casual
***
Junior Member
315 posts

Joined: Jan 2003
if you wanna count number occurance of a character in a file

1. have a hashmap something like a 2 dimensional array
2. read from the file example 512 characters at a time
3. for each character look in the hashmap for its existence(key)
4. if it doesn't exist, add the character to the hashmap and +1 to its value, if it already exist, +1 to its value
5. when all the characters in the file are processed, you'll have a hashmap with all the characters in the file and their count

This post has been edited by asc2: Mar 4 2006, 11:29 AM
TSdeehanie
post Mar 4 2006, 04:33 PM

New Member
*
Junior Member
14 posts

Joined: Jan 2006
QUOTE(silverhawk @ Mar 4 2006, 01:14 AM)
Read from the file and put the contents into a string variable and read it the same way you would from a textbox.
*
I've tried this and its work! Thanks .. but the problem is my counting result(i've used listbox to display) and it still counting the previous value of the previous textbox input. I've put the clear button to clear all the listed result( lstResult.Clear) but i guess it's only clear what is in the listbox, not to reset the previous value.
wKkaY
post Mar 4 2006, 05:17 PM

misutā supākoru
Group Icon
VIP
6,008 posts

Joined: Jan 2003
QUOTE(anthony_yio @ Mar 4 2006, 01:59 PM)
Actually, there is no worry for modern day OS, system will proceed to virtual memory. But that is only for counting number of char,  lol.
*

Yes, but paging to virtual memory is the worst thing that can happen for this application, as you're trying to read from disk and at the same time paging out!

Why let yourself be memory-bound when you don't have to? smile.gif
anthony_yio
post Mar 4 2006, 05:29 PM

........
Group Icon
Elite
1,828 posts

Joined: Jan 2003


QUOTE(wKkaY @ Mar 4 2006, 05:17 PM)
Yes, but  paging to virtual memory is the worst thing that can happen for this application, as you're trying to read from disk and at the same time paging out!

Why let yourself be memory-bound when you don't have to? smile.gif
*
Of course I know that. That is why I end my reply with lol (laugh out loud) (sarcastically)

Many programs nowadays "can" run without much problems even if they leak or inefficiently been programmed. Modern OS take care of all the problems for them, unlike those Win9x days that require a restart of OS if the program leak and etc. Now, all they need to do is just close the application and start again (for leak), all the memory get reclaimed. Even if they use up all the memory, OS still take care of the mess with virtual memory.

And what next is the .NET with a garbage collector. Even ppl with zero knowledge on memory paging concept can program now. lol




wKkaY
post Mar 4 2006, 09:56 PM

misutā supākoru
Group Icon
VIP
6,008 posts

Joined: Jan 2003
Righto, so let's declare all our string variables a gigabyte long even if they're to store a single character, because the OS handles them "without much problem".
anthony_yio
post Mar 6 2006, 10:54 AM

........
Group Icon
Elite
1,828 posts

Joined: Jan 2003


QUOTE(wKkaY @ Mar 4 2006, 09:56 PM)
Righto, so let's declare all our string variables a gigabyte long even if they're to store a single character, because the OS handles them "without much problem".
*
Err, I was just trying to explain why things still run that way. (eventhough it is not efficient)

Programming in old days will be "forced" to make things run as efficient as possible because they are clearly out of choices. (hardware spec was low those days. )

But now, the PC is so powerful that literally, anything goes even without the understanding in programming.

That is why, I was trying to explain why it will still work "without much problems" but of course again, it is not encouraged.



SincerePrayer
post Mar 7 2006, 11:22 AM

love to pray
Group Icon
Elite
1,120 posts

Joined: Jun 2005
wKkaY & anthony_yio,

You guys both got the point. The design of the program is not efficient but is still workable. Let's try not to give deehanie a freak. tongue.gif



deehanie,

You might want to reset the array and the variables that hold any values during the search.



Happy Coding and Sharing smile.gif

 

Change to:
| Lo-Fi Version
0.0174sec    0.63    5 queries    GZIP Disabled
Time is now: 24th December 2025 - 09:28 PM