How to code a counter?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mas Juliza Alias
    New Member
    • Aug 2010
    • 67

    How to code a counter?

    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:
    Code:
    string                   counter
    --------------------------------
    POINTS_CON               1
    POINTS_CON               1
    POINTS_ICON              2
    POINTS_ICON              2
    POINTS_ICON              2
    POINTS_CON               3
    POINTS_ICON              4
    POINTS_CON               5
    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
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

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

    see also attachment
    Code:
    Option Explicit
    Private Type ARRAYDATAvars
       STRING As String
       COUNTER As Integer
    End Type
    
    Private Sub ComOpen_Click()
    Dim FILENUM As Integer
    Dim INPUTTEXT As String
    Dim ARRAYDATA() As ARRAYDATAvars
    Dim ARRAYDATAidx As Integer
    '§ read file
       FILENUM = FreeFile
       ReDim ARRAYDATA(0)
       On Error GoTo Error_Open_File
       Open App.Path & "\tapahdam_surfacearea.txt" For Input As #FILENUM
          Do Until EOF(FILENUM)
             Line Input #FILENUM, INPUTTEXT
             If InStr(INPUTTEXT, "POINTS_CON") Then
                ReDim Preserve ARRAYDATA(UBound(ARRAYDATA) + 1)
                With ARRAYDATA(UBound(ARRAYDATA))
                   .STRING = "POINTS_CON"
                   If UBound(ARRAYDATA) = 1 Then '§ if it's the first line: count = 1
                      .COUNTER = 1
                   Else
                      If ARRAYDATA(UBound(ARRAYDATA) - 1).STRING = "POINTS_CON" Then
                         '§ if it's also POINTS_CON the counter = previous
                         .COUNTER = ARRAYDATA(UBound(ARRAYDATA) - 1).COUNTER
                      Else
                         '§ if it's not POINTS_CON the counter = previous +1
                         .COUNTER = ARRAYDATA(UBound(ARRAYDATA) - 1).COUNTER + 1
                      End If
                   End If
                End With
             ElseIf InStr(INPUTTEXT, "POINTS_ICON") Then
                ReDim Preserve ARRAYDATA(UBound(ARRAYDATA) + 1)
                With ARRAYDATA(UBound(ARRAYDATA))
                   .STRING = "POINTS_ICON"
                   If UBound(ARRAYDATA) = 1 Then '§ if it's the first line: count = 1
                      .COUNTER = 1
                   Else
                      If ARRAYDATA(UBound(ARRAYDATA) - 1).STRING = "POINTS_ICON" Then
                         '§ if it's also POINTS_ICON the counter = previous
                         .COUNTER = ARRAYDATA(UBound(ARRAYDATA) - 1).COUNTER
                      Else
                         '§ if it's not POINTS_ICON the counter = previous +1
                         .COUNTER = ARRAYDATA(UBound(ARRAYDATA) - 1).COUNTER + 1
                      End If
                   End If
                End With
             End If
          Loop
       Close #FILENUM
    '§ dump data in textbox
       For ARRAYDATAidx = 1 To UBound(ARRAYDATA)
          With ARRAYDATA(ARRAYDATAidx)
             Text1.Text = Text1.Text & .STRING & vbTab & .COUNTER & vbNewLine
          End With
       Next
    Exit Sub
    Error_Open_File:
       Close #FILENUM
       MsgBox "There is an error opening the file"
    End Sub
    Attached Files

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      If you have a minute, please let us know whether this resolved your problem.

      Comment

      • Mas Juliza Alias
        New Member
        • Aug 2010
        • 67

        #4
        Yes! It is solved successfully. Thanks to Guido Geurs!

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          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.
          Code:
          Option Explicit
          DefLng A-Z
           
          Private Sub ComOpen_Click()
            Dim FILENUM As Integer
            Dim INPUTTEXT As String
            Dim LastString As String, ThisString As String, Counter As Long
            ' Read file
            FILENUM = FreeFile
            On Error GoTo Error_Open_File
            Open App.Path & "\tapahdam_surfacearea.txt" For Input Access Read Shared As #FILENUM
            On Error Goto 0
            Do Until EOF(FILENUM)
              Line Input #FILENUM, INPUTTEXT
              ThisString = Trim$(Mid$(INPUTTEXT, 41, 11))
              ' Only look at lines containing our string.
              If Left$(ThisString, 7) = "POINTS_" Then
                If ThisString <> LastString Then
                  Counter = Counter + 1
                  LastString = ThisString
                End If
                ' Output results to text box.
                Text1.Text = Text1.Text & ThisString & vbTab & Counter & vbNewLine
              End If
            Loop
            Close #FILENUM
            Exit Sub
          
          Error_Open_File:
            MsgBox "There is an error opening the file"
          End Sub
          Attached Files

          Comment

          • Mas Juliza Alias
            New Member
            • Aug 2010
            • 67

            #6
            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:
            Code:
            Open App.Path & "\tapahdam_surfacearea.txt" For Input Access Read Shared As #FILENUM
            Code:
            Open App.Path & "\tapahdam_surfacearea.txt" For Input As #FILENUM
            What is 'Access Read Shared' used for?

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              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.
              Last edited by Killer42; Jun 17 '11, 06:42 AM. Reason: Added the last paragraph.

              Comment

              Working...