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: - 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.
Put the lines in an array and check with the previous "string".
If it's different: count= previous count +1.
see also attachment - 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
-
6 1659
Put the lines in an array and check with the previous "string".
If it's different: count= previous count +1.
see also attachment - 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
-
If you have a minute, please let us know whether this resolved your problem.
Yes! It is solved successfully. Thanks to Guido Geurs!
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. - 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
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: - Open App.Path & "\tapahdam_surfacearea.txt" For Input Access Read Shared As #FILENUM
- Open App.Path & "\tapahdam_surfacearea.txt" For Input As #FILENUM
What is 'Access Read Shared' used for?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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
|
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 ...
|
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...
|
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...
|
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,...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |