Solved

occurences

Posted on 2002-04-20
4
252 Views
Last Modified: 2010-05-02
I've got a lot of lines in a text file like these:

23544,50,50
43443,40,30
23344,60,70
23544,50,22
43443,40,50
and so on...

Now I would like to count up the number of occurences of each first value on the line.

0
Comment
Question by:michael123199
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Expert Comment

by:TomLaw1999
ID: 6956191
1) Open the text file
2) Read in the values one line at a time
3) Sort the lines into numeric order
4) Count the grouped lines based upon the first value.

Are you after pseudo code or do you need help with a specific portion of the program?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6956244
Assuming that the values are all integers than the following should work (Modify RANGE for performance):

Private Type ValueType
   Value As Long
   Counter As Long
End Type

Private Const RANGE = 100
Private Sub Command1_Click()
Dim Handle As Integer
Dim lValue1 As Long
Dim lValue2 As Long
Dim lValue3 As Long

ReDim aValues(1 To RANGE) As ValueType
Handle = FreeFile
Open "your file name goes here" For Input As Handle
Do While Not EOF(Handle)
   Input #Handle, lValue1, lValue2, lValue3
   AddValueCounter aValues(), lValue1
Loop
ShowResults aValues()
Close Handle

End Sub

Private Sub AddValueCounter(aValues() As ValueType, ByVal lValue As Long)
Dim Found As Boolean
Dim I As Integer

Found = False
I = 1
Do While Not Found
   If I > UBound(aValues) Then
      ReDim Preserve aValues(1 To I + RANGE) As ValueType
   End If
   If aValues(I).Value = 0 Then
      aValues(I).Value = lValue
      aValues(I).Counter = 1
      Found = True
   ElseIf aValues(I).Value = lValue Then
      aValues(I).Counter = aValues(I).Counter + 1
      Found = True
   Else
      I = I + 1
   End If
Loop

End Sub

Private Sub ShowResults(aValues() As ValueType)
Dim Done As Boolean
Dim I As Integer

Done = False
I = 1
Do While Not Done
   If I > UBound(aValues) Then
      Done = True
   ElseIf aValues(I).Value Then
      Debug.Print aValues(I).Value, aValues(I).Counter
      I = I + 1
   Else
      Done = True
   End If
Loop

End Sub

Anthony
0
 

Author Comment

by:michael123199
ID: 6956785
ok, tnx anthony so far.. but i ran your code and nothing happened (i'm a vb rookie)... I altered the line Open "your file name goes here" For Input As Handle
to Open "c:\test.txt" For Input As Handle

and made the file test.txt with as content:
23544,50,50
43443,40,30
23344,60,70
23544,50,22
43443,40,50

Can you please help me out ?



0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 50 total points
ID: 6956803
Here goes step instructions:
1. Start a new project
2. Add a Command Button (called Command1)
3. Paste the code I posted, but make the file name match yours.
4. Run the code.
5. Press the Command button (Command1)
6. Take a look at the Immediate window, the results should be as follows:
 23544         2
 43443         2
 23344         1

If you are still having difficulties. Try changing the following line:

Debug.Print aValues(I).Value, aValues(I).Counter

to

MsgBox CStr(aValues(I).Value) & "-->" & CStr(aValues(I).Counter)

This will display each value and the count separately in a MsgBox.

Anthony
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access 2013 combo box not working 3 76
vb6 connector to mongodb 2 163
How to set the row selection as it was prior leaving the datagrid in vb6 3 61
RUNRMTCMD from AS/400 13 130
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question