473,387 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to code a counter?

Mas Juliza Alias
Hi,
I have a data file with 2 different strings, named as CON and ICON. How to turn the value of each line to integer that work as a counter for each change of string name? For example:
Expand|Select|Wrap|Line Numbers
  1. string                   counter
  2. --------------------------------
  3. POINTS_CON               1
  4. POINTS_CON               1
  5. POINTS_ICON              2
  6. POINTS_ICON              2
  7. POINTS_ICON              2
  8. POINTS_CON               3
  9. POINTS_ICON              4
  10. POINTS_CON               5
  11. POINTS_CON               5
Number of lines with the same string is not fixed. Attached is the file I use as input where I want to add a new column on the right for the counter in the output file.
Attached Files
File Type: txt tapahdam_surfacearea.txt (8.5 KB, 298 views)
May 26 '11 #1

✓ answered by Guido Geurs

Put the lines in an array and check with the previous "string".
If it's different: count= previous count +1.

see also attachment
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Private Type ARRAYDATAvars
  3.    STRING As String
  4.    COUNTER As Integer
  5. End Type
  6.  
  7. Private Sub ComOpen_Click()
  8. Dim FILENUM As Integer
  9. Dim INPUTTEXT As String
  10. Dim ARRAYDATA() As ARRAYDATAvars
  11. Dim ARRAYDATAidx As Integer
  12. '§ read file
  13.    FILENUM = FreeFile
  14.    ReDim ARRAYDATA(0)
  15.    On Error GoTo Error_Open_File
  16.    Open App.Path & "\tapahdam_surfacearea.txt" For Input As #FILENUM
  17.       Do Until EOF(FILENUM)
  18.          Line Input #FILENUM, INPUTTEXT
  19.          If InStr(INPUTTEXT, "POINTS_CON") Then
  20.             ReDim Preserve ARRAYDATA(UBound(ARRAYDATA) + 1)
  21.             With ARRAYDATA(UBound(ARRAYDATA))
  22.                .STRING = "POINTS_CON"
  23.                If UBound(ARRAYDATA) = 1 Then '§ if it's the first line: count = 1
  24.                   .COUNTER = 1
  25.                Else
  26.                   If ARRAYDATA(UBound(ARRAYDATA) - 1).STRING = "POINTS_CON" Then
  27.                      '§ if it's also POINTS_CON the counter = previous
  28.                      .COUNTER = ARRAYDATA(UBound(ARRAYDATA) - 1).COUNTER
  29.                   Else
  30.                      '§ if it's not POINTS_CON the counter = previous +1
  31.                      .COUNTER = ARRAYDATA(UBound(ARRAYDATA) - 1).COUNTER + 1
  32.                   End If
  33.                End If
  34.             End With
  35.          ElseIf InStr(INPUTTEXT, "POINTS_ICON") Then
  36.             ReDim Preserve ARRAYDATA(UBound(ARRAYDATA) + 1)
  37.             With ARRAYDATA(UBound(ARRAYDATA))
  38.                .STRING = "POINTS_ICON"
  39.                If UBound(ARRAYDATA) = 1 Then '§ if it's the first line: count = 1
  40.                   .COUNTER = 1
  41.                Else
  42.                   If ARRAYDATA(UBound(ARRAYDATA) - 1).STRING = "POINTS_ICON" Then
  43.                      '§ if it's also POINTS_ICON the counter = previous
  44.                      .COUNTER = ARRAYDATA(UBound(ARRAYDATA) - 1).COUNTER
  45.                   Else
  46.                      '§ if it's not POINTS_ICON the counter = previous +1
  47.                      .COUNTER = ARRAYDATA(UBound(ARRAYDATA) - 1).COUNTER + 1
  48.                   End If
  49.                End If
  50.             End With
  51.          End If
  52.       Loop
  53.    Close #FILENUM
  54. '§ dump data in textbox
  55.    For ARRAYDATAidx = 1 To UBound(ARRAYDATA)
  56.       With ARRAYDATA(ARRAYDATAidx)
  57.          Text1.Text = Text1.Text & .STRING & vbTab & .COUNTER & vbNewLine
  58.       End With
  59.    Next
  60. Exit Sub
  61. Error_Open_File:
  62.    Close #FILENUM
  63.    MsgBox "There is an error opening the file"
  64. End Sub
  65.  

6 1659
Guido Geurs
767 Expert 512MB
Put the lines in an array and check with the previous "string".
If it's different: count= previous count +1.

see also attachment
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Private Type ARRAYDATAvars
  3.    STRING As String
  4.    COUNTER As Integer
  5. End Type
  6.  
  7. Private Sub ComOpen_Click()
  8. Dim FILENUM As Integer
  9. Dim INPUTTEXT As String
  10. Dim ARRAYDATA() As ARRAYDATAvars
  11. Dim ARRAYDATAidx As Integer
  12. '§ read file
  13.    FILENUM = FreeFile
  14.    ReDim ARRAYDATA(0)
  15.    On Error GoTo Error_Open_File
  16.    Open App.Path & "\tapahdam_surfacearea.txt" For Input As #FILENUM
  17.       Do Until EOF(FILENUM)
  18.          Line Input #FILENUM, INPUTTEXT
  19.          If InStr(INPUTTEXT, "POINTS_CON") Then
  20.             ReDim Preserve ARRAYDATA(UBound(ARRAYDATA) + 1)
  21.             With ARRAYDATA(UBound(ARRAYDATA))
  22.                .STRING = "POINTS_CON"
  23.                If UBound(ARRAYDATA) = 1 Then '§ if it's the first line: count = 1
  24.                   .COUNTER = 1
  25.                Else
  26.                   If ARRAYDATA(UBound(ARRAYDATA) - 1).STRING = "POINTS_CON" Then
  27.                      '§ if it's also POINTS_CON the counter = previous
  28.                      .COUNTER = ARRAYDATA(UBound(ARRAYDATA) - 1).COUNTER
  29.                   Else
  30.                      '§ if it's not POINTS_CON the counter = previous +1
  31.                      .COUNTER = ARRAYDATA(UBound(ARRAYDATA) - 1).COUNTER + 1
  32.                   End If
  33.                End If
  34.             End With
  35.          ElseIf InStr(INPUTTEXT, "POINTS_ICON") Then
  36.             ReDim Preserve ARRAYDATA(UBound(ARRAYDATA) + 1)
  37.             With ARRAYDATA(UBound(ARRAYDATA))
  38.                .STRING = "POINTS_ICON"
  39.                If UBound(ARRAYDATA) = 1 Then '§ if it's the first line: count = 1
  40.                   .COUNTER = 1
  41.                Else
  42.                   If ARRAYDATA(UBound(ARRAYDATA) - 1).STRING = "POINTS_ICON" Then
  43.                      '§ if it's also POINTS_ICON the counter = previous
  44.                      .COUNTER = ARRAYDATA(UBound(ARRAYDATA) - 1).COUNTER
  45.                   Else
  46.                      '§ if it's not POINTS_ICON the counter = previous +1
  47.                      .COUNTER = ARRAYDATA(UBound(ARRAYDATA) - 1).COUNTER + 1
  48.                   End If
  49.                End If
  50.             End With
  51.          End If
  52.       Loop
  53.    Close #FILENUM
  54. '§ dump data in textbox
  55.    For ARRAYDATAidx = 1 To UBound(ARRAYDATA)
  56.       With ARRAYDATA(ARRAYDATAidx)
  57.          Text1.Text = Text1.Text & .STRING & vbTab & .COUNTER & vbNewLine
  58.       End With
  59.    Next
  60. Exit Sub
  61. Error_Open_File:
  62.    Close #FILENUM
  63.    MsgBox "There is an error opening the file"
  64. End Sub
  65.  
Attached Files
File Type: zip How to code a counter v1.0.0.zip (3.2 KB, 90 views)
May 26 '11 #2
Killer42
8,435 Expert 8TB
If you have a minute, please let us know whether this resolved your problem.
Jun 16 '11 #3
Yes! It is solved successfully. Thanks to Guido Geurs!
Jun 16 '11 #4
Killer42
8,435 Expert 8TB
Excellent!

You might also want to consider a simpler version, without the array. Neither way is better, it's just a reminder that there's always more than one way of solving a programming issue.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. DefLng A-Z
  3.  
  4. Private Sub ComOpen_Click()
  5.   Dim FILENUM As Integer
  6.   Dim INPUTTEXT As String
  7.   Dim LastString As String, ThisString As String, Counter As Long
  8.   ' Read file
  9.   FILENUM = FreeFile
  10.   On Error GoTo Error_Open_File
  11.   Open App.Path & "\tapahdam_surfacearea.txt" For Input Access Read Shared As #FILENUM
  12.   On Error Goto 0
  13.   Do Until EOF(FILENUM)
  14.     Line Input #FILENUM, INPUTTEXT
  15.     ThisString = Trim$(Mid$(INPUTTEXT, 41, 11))
  16.     ' Only look at lines containing our string.
  17.     If Left$(ThisString, 7) = "POINTS_" Then
  18.       If ThisString <> LastString Then
  19.         Counter = Counter + 1
  20.         LastString = ThisString
  21.       End If
  22.       ' Output results to text box.
  23.       Text1.Text = Text1.Text & ThisString & vbTab & Counter & vbNewLine
  24.     End If
  25.   Loop
  26.   Close #FILENUM
  27.   Exit Sub
  28.  
  29. Error_Open_File:
  30.   MsgBox "There is an error opening the file"
  31. End Sub
Attached Files
File Type: zip Form1.zip (918 Bytes, 77 views)
Jun 16 '11 #5
Your code is easier for a beginner like me to understand, and it works nicely. Thank YOU!
If you have time, can you explain to me the different between these lines:
Expand|Select|Wrap|Line Numbers
  1. Open App.Path & "\tapahdam_surfacearea.txt" For Input Access Read Shared As #FILENUM
Expand|Select|Wrap|Line Numbers
  1. Open App.Path & "\tapahdam_surfacearea.txt" For Input As #FILENUM
What is 'Access Read Shared' used for?
Jun 16 '11 #6
Killer42
8,435 Expert 8TB
As is so often the case, it's a matter of personal preferences.

The two Open statements most likely do exactly the same thing. I've included the full set of options to force it to work the way I want. Guido left them out, so VB just uses the default settings.

This merely reflects the fact that I never remember what the defaults are.

Shared means that other people/programs can open the file for reading or writing while you're using it. Access Read means this program will only be allowed to read the file, not update it. Check your online help for all the options on the Open statement.
Jun 17 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Leif K-Brooks | last post by:
I got bored with working on larger projects and wrote a little script/module for counting logical (not physical) lines of Python code in a file or directory. It uses ASTs generated by...
1
by: Ken Fine | last post by:
I have a menu system that has nodes that can be opened or closed. In an effort to make my code more manageable, I programmed a little widget tonight that keeps track of the open/active item and...
3
by: Rv5 | last post by:
I have an assignment due mid next week that I have completed. I was hoping someone could take a look at the code and tell me what they think of the style. Id like to know if this is good code...
1
by: lily82 | last post by:
can sm 1 help me transform this code to C# code?? tq so much :wink: Goto : <% Dim counter Dim page Dim pages counter= 10 pages = 20 page = 1
6
by: Calligra | last post by:
have a form which looks at the title of the workbook saved on a different workbook, determines whether or not information has been inputted previously and then asks the user if the information is...
3
blackstormdragon
by: blackstormdragon | last post by:
Here were our instructions: "My mother always took a little red counter to the grocery store. The counter was used to keep tally of the amount of money she would have spent so far on that visit to...
1
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
16
by: lovecreatesbea... | last post by:
It takes mu so time to finish this C source code line count function. What do you think about it? / ******************************************************************************* * Function ...
2
by: gumbercules | last post by:
I am designing a site that is regulary updated with new podcasts. I would like to be able to have a counter next to each podcast showing how many hits/listens/plays/views each particular podcast has...
1
by: joseph terzi | last post by:
I am trying to use recursion to calculate the nth prime. It works up until i believe around 300 when the interpreter says "maximum recusrion depth exceeded." Here is the code i wrote. def...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.