Hello guys,
I have a little bit of problem with textfile operations.I would like to some help from you smart people in the forum.
I have a textfile by the name of 10117053.swd. This file contains a certain data in specific format like
010110010001ADITY3104200310450001
010110010001ADITY3104600310460001
010110010001ADITY3104800310490001
010110010001DD8 3202000320200001
010110010001ENAD 3161100316280001
010110010001ENAD 3191400320060001
010110010001GEMI 3143500314350001
010110010001GEMI 3152400315260001
010110010001HBO 3143600314360001
010110010001SANSK3113300311330001
010110010001SITMU3092400310240001
010110010001TEJNW3103900310400001
010110010001TEJNW3104700310470001
010110010004DD8 3072000307200001
010110010004DD8 3202000320200001
010110010004ENAD 3080800308080001
010110010004ENAD 3083400308470001
010110010004GEMI 3074200308030001
010110010004GEMI 3080600308070001
010110010004SITMU3065300306530001
010110010004SITMU3070000307000001
* *
the first 12 numbers as homeid and the next 5 letters are channelname
now i need to give the count of unique channels that are watched by the homeid in a new txtfile in the format
homeid - count of unique channels that are watched by this id.
for suppose homeid 010110010001 watched ADITY TWICE AND OTHER CHANNELS,BUT WE WANT THE COUNT OF THE UNIQUE CHANNELS THAT ARE WATCHED BY THESE HOMEID LIKE FOR ABOVE HOMEID 010110010001 HAS WATCHED 8 UNIQUE CHANNELS SO ON FOR EACH HOMEID.
THE NEW TXTFILE SHOULD BE HOMEID
010110010001 8
010110010004 4 AND SO ON.
NOW I AM NOT ABLE TO GET THIS i HAVE THE CODE pLEASE HAVE THE LOOK AT THE CODE -
Option Explicit
-
Dim fsys As New FileSystemObject
-
Dim txtstream As TextStream
-
Dim outstream As TextStream
-
-
-
Private Sub cmdclick_Click()
-
-
Dim j As Integer
-
Dim searchline As String ' reads each line of txtstream
-
Dim filename As String 'file name which is dynamically generated
-
Dim homeid As String 'to store 12 numbers
-
Dim prevhomeid As String ' to comapre with other numbers
-
Dim channelname As String ' to store channelname
-
Dim prevchannelname As String ' to compare prevchannelname
-
Dim channelcount As Integer ' to give channelcount
-
-
-
-
If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
-
MsgBox "file is open"
-
Else
-
MsgBox "file not found"
-
End If
-
-
prevchannelname = ""
-
prevhomeid = ""
-
channelcount = 0
-
-
Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
-
Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
-
Do Until txtstream.AtEndOfStream
-
searchline = txtstream.ReadLine
-
homeid = Mid(searchline, 1, 10)
-
If prevhomeid <> homeid Then
-
'do nothing`
-
outstream.WriteLine (homeid)
-
End If
-
prevhomeid = homeid
-
Loop
-
-
MsgBox " Process completed"
-
End Sub
-
any help would be sincerely appreciated.
42 10257
...
NOW I AM NOT ABLE TO GET THIS i HAVE THE CODE pLEASE HAVE THE LOOK AT THE CODE - Option Explicit
-
Dim fsys As New FileSystemObject
-
Dim txtstream As TextStream
-
Dim outstream As TextStream
-
-
Private Sub cmdclick_Click()
-
-
Dim j As Integer
-
Dim searchline As String ' reads each line of txtstream
-
Dim filename As String ' file name which is dynamically generated
-
Dim homeid As String ' to store 12 numbers
-
Dim prevhomeid As String ' to comapre with other numbers
-
Dim channelname As String ' to store channelname
-
Dim prevchannelname As String ' to compare prevchannelname
-
Dim channelcount As Integer ' to give channelcount
-
-
-
-
If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
-
MsgBox "file is open"
-
Else
-
MsgBox "file not found"
-
End If
-
-
prevchannelname = ""
-
prevhomeid = ""
-
channelcount = 0
-
-
Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
-
Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
-
Do Until txtstream.AtEndOfStream
-
searchline = txtstream.ReadLine
-
homeid = Mid(searchline, 1, 10)
-
If prevhomeid <> homeid Then
-
'do nothing`
-
outstream.WriteLine (homeid)
-
End If
-
prevhomeid = homeid
-
Loop
-
-
MsgBox " Process completed"
-
End Sub
Can you give us a clue as to what you don't like about your code? What does it do wrong, or not do?
One or two things did occur to me when I glanced at the code... - Your code assumes the file is sorted. Is that a reasonable assumption in this case?
- You are picking up the first 10 characters as the homeid, but you told us it was 12.
- Assuming the file is sorted, you look as though you're probably picking up each homeid alright. Within each one, I guess you just need to do a similar check for previous channel and report each new one which shows up.
-
-
-
Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
-
Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
-
Do Until txtstream.AtEndOfStream
-
searchline = txtstream.ReadLine
-
homeid = Mid(searchline, 1, 10)
- 'here check this homeid with the distinct homeid in array say prevHomeId()
-
'if this home id doesnot exists then add this to array
-
'then again run this loop here u will check home id with your arraylist
-
' increment the counter and finally u'll get the ans
-
If prevhomeid <> homeid Then
-
'do nothing`
-
outstream.WriteLine (homeid)
-
End If
-
prevhomeid = homeid
-
Loop
-
-
MsgBox " Process completed"
-
End Sub
-
-
-
-
Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
-
Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
-
Do Until txtstream.AtEndOfStream
-
searchline = txtstream.ReadLine
-
homeid = Mid(searchline, 1, 10)
- 'here check this homeid with the distinct homeid in array say prevHomeId()
-
'if this home id doesnot exists then add this to array
-
'then again run this loop here u will check home id with your arraylist
-
' increment the counter and finally u'll get the ans
-
If prevhomeid <> homeid Then
-
'do nothing`
-
outstream.WriteLine (homeid)
-
End If
-
prevhomeid = homeid
-
Loop
-
-
MsgBox " Process completed"
-
End Sub
-
Sir,you are right that i should use an array ,but my problem is that i do not know how to put the homeid value in arrays and compare them,basically i know what are arrays but ,how they store data or manipulate data, i have very little knowledge,perhaps any help on this would be very useful for me.
Right know i am goind through a topic on arrays and lets see if i am able to do this.Thank you sir for your help.
Sir,you are right that i should use an array ,but my problem is that i do not know how to put the homeid value in arrays and compare them,basically i know what are arrays but ,how they store data or manipulate data, i have very little knowledge,perhaps any help on this would be very useful for me.
Right know i am goind through a topic on arrays and lets see if i am able to do this.Thank you sir for your help.
Keep us posted on how it goes.
However, as long as the items are definitely in sorted sequence, you don't need to use an array - your technique of checking against the previous item will work fine. You just need to extend it to handle the channels in a similar way.
On the other hand, if they are not sorted, then yes, you will need to use something like an array to keep track of those which have already been handled.
Keep us posted on how it goes.
However, as long as the items are definitely in sorted sequence, you don't need to use an array - your technique of checking against the previous item will work fine. You just need to extend it to handle the channels in a similar way.
On the other hand, if they are not sorted, then yes, you will need to use something like an array to keep track of those which have already been handled.
Sir, It seems I am not able to clearly define what i actually want. so let me try it once again
Sir ,I have textfile which have name like "10117051.swd" now this is a file which contains data of television audio Ratings.
The data in this file have systematic pattern so that i can identify it.
for example take a look at this line
010110010001ADITY1114700111470001
now the first 12 numbers ie 010110010001 are homeids followed channel name ie ADITY.
Now sir yes,u r right that these file are sorted,but I do not want to sort these file
I want a code which can give the count of unique channels that is watched by this id in specific format in a new textfile which should have the data like
010110010001 8 where 8 is the number of unique channels watched by this id and so on for every unique homeid.
now sir,my biggest concern is i am not good with arrays and how to use them,hence i am facing problem.
if you could guide me, i would be very thankful.
Thank you
Keep us posted on how it goes.
However, as long as the items are definitely in sorted sequence, you don't need to use an array - your technique of checking against the previous item will work fine. You just need to extend it to handle the channels in a similar way.
On the other hand, if they are not sorted, then yes, you will need to use something like an array to keep track of those which have already been handled.
sir I m trying to implement arrays in my code but i am unable to do so,pls help me.here is the sample code that i m trying to do
[code]
Private Sub cmdclick_Click()
Dim j As Integer
Dim searchline As String
Dim filename As String
Dim homeid As String
Dim prevhomeid As String
Dim channelname As String
Dim prevchannelname As String
Dim channelcount As Integer
Dim channelarray() As String
For j = 0 To lstmarket.ListCount - 1
If lstmarket.Selected(j) = True Then
If Len(Trim(txtyear.Text)) > 2 Or IsNumeric(Trim(txtyear.Text)) = False Then
MsgBox "this should be 2 digit number "
txtyear.Text = ""
txtyear.SetFocus
Exit Sub
End If
If Trim(txtweek) <= 9 And Len(Trim(txtweek)) < 2 Or IsNumeric(txtweek.Text) = False Then
txtweek = "0" & Trim(txtweek)
End If
If Trim(txtday) > 7 Or IsNumeric(txtday) = False Then
MsgBox "Please enter number which is equal to or less than 7"
txtday.Text = ""
txtday.SetFocus
Exit Sub
End If
filename = lstmarket.List(j) & Trim(txtyear) & Trim(txtweek) & Trim(txtday) & ".swd"
MsgBox ("you have selected the following" & filename)
End If
Next j
'********main code for listing all the channels that the house id watch*******
If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
MsgBox "file is open"
Else
MsgBox "file not found"
End If
prevchannelname = ""
prevhomeid = ""
channelcount = 0
Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
Do Until txtstream.AtEndOfStream
searchline = txtstream.ReadLine
homeid = Mid(searchline, 1, 10)
'I am tring put values into the array but it is not going in instead it is giving err like cannot assign an array
'here is what i ma trying to do
'channelarray(searchline)=channelarray
i's this right
any examples or help would be very helpful
'prevhomeid (homeid)
If prevhomeid <> homeid Then
'do nothing`
channelname = Mid(homeid, 12, 5)
If prevchannelname <> channelname Then
channelcount = channelcount + 1
End If
outstream.WriteLine (homeid)
End If
prevhomeid = homeid
Loop
MsgBox " Process completed"
End Sub
[\code]
have a look at this if this is the right way. -
-
Option Explicit
-
Dim fsys As New FileSystemObject
-
Dim txtstream As TextStream
-
Dim outstream As TextStream
-
-
-
Private Sub cmdclick_Click()
-
Dim j As Integer
-
Dim searchline As String
-
Dim filename As String
-
Dim homeid As String
-
Dim prevhomeid As String
-
Dim channelname As String
-
Dim prevchannelname As String
-
Dim channelcount As Integer
-
Dim channelarray(100000) As String
-
Dim i As Integer
-
-
For j = 0 To lstmarket.ListCount - 1
-
If lstmarket.Selected(j) = True Then
-
-
-
If Len(Trim(txtyear.Text)) > 2 Or IsNumeric(Trim(txtyear.Text)) = False Then
-
MsgBox "this should be 2 digit number "
-
txtyear.Text = ""
-
txtyear.SetFocus
-
Exit Sub
-
End If
-
-
If Trim(txtweek) <= 9 And Len(Trim(txtweek)) < 2 Or IsNumeric(txtweek.Text) = False Then
-
txtweek = "0" & Trim(txtweek)
-
End If
-
-
If Trim(txtday) > 7 Or IsNumeric(txtday) = False Then
-
MsgBox "Please enter number which is equal to or less than 7"
-
txtday.Text = ""
-
txtday.SetFocus
-
Exit Sub
-
End If
-
-
filename = lstmarket.List(j) & Trim(txtyear) & Trim(txtweek) & Trim(txtday) & ".swd"
-
MsgBox ("you have selected the following" & filename)
-
End If
-
Next j
-
-
'********main code for listing all the channels that the house id watch*******
-
-
If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
-
MsgBox "file is open"
-
Else
-
MsgBox "file not found"
-
End If
-
-
prevchannelname = ""
-
prevhomeid = ""
-
-
channelcount = 0
-
-
Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
-
Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
-
Do Until txtstream.AtEndOfStream
-
searchline = txtstream.ReadLine
-
homeid = Mid(searchline, 1, 10)
-
For i = 0 To channelcount
-
If channelarray(i) <> homeid Then
-
channelarray(i) = homeid
-
End If
-
If homeid <> channelarray(i) Then
-
channelcount = channelcount + 1
-
'channelcount = channelcount + 1
-
End If
-
-
Next i
-
'prevhomeid (homeid)
-
'If prevhomeid <> homeid Then
-
'If channelarray(i) <> homeid Then
-
-
-
'do nothing`
-
'channelname = Mid(homeid, 12, 5)
-
'If prevchannelname <> channelname Then
-
'channelcount = channelcount + 1
-
'End If
-
'outstream.WriteLine (homeid)
-
outstream.WriteLine (homeid) & vbTab & channelcount
-
' End If
-
'prevhomeid = homeid
-
Loop
-
-
MsgBox " Process completed"
-
End Sub
-
-
still i am not able to get the desired output as mentioned above
I'm short on time, so I'm going to address one thing at a time. This piece of code... - For i = 0 To channelcount
-
If channelarray(i) <> homeid Then
-
channelarray(i) = homeid
-
End If
-
If homeid <> channelarray(i) Then
-
channelcount = channelcount + 1
-
End If
-
Next i
...has some serious problems. - For starters, is this array supposed to hold channels, or homeids? If the former, then you've used it in the wrong place. If the latter, then the name is confusing.
- If you follow the execution you will find that it is simply going to place each homeid in turn into the first (the "zeroth") occurence of the array.
- The second condition tested (homeid <> channelarray(i)) can never possibly be True, since you have just just set the two variables equal.
That's enough for today's lesson. :)
I'm short on time, so I'm going to address one thing at a time. This piece of code... - For i = 0 To channelcount
-
If channelarray(i) <> homeid Then
-
channelarray(i) = homeid
-
End If
-
If homeid <> channelarray(i) Then
-
channelcount = channelcount + 1
-
End If
-
Next i
...has some serious problems.- For starters, is this array supposed to hold channels, or homeids? If the former, then you've used it in the wrong place. If the latter, then the name is confusing.
- If you follow the execution you will find that it is simply going to place each homeid in turn into the first (the "zeroth") occurence of the array.
- The second condition tested (homeid <> channelarray(i)) can never possibly be True, since you have just just set the two variables equal.
That's enough for today's lesson. :)
thnks for the reply,now let me thnk once again I have a textfile which has some homeids and channelname .
like
010110010001DD8 5073100507310001
010110010001DD8 5073100507310001
010110010001ENAD 5073000507300001
010110010001ETV2 5073200507330001
010110010001ETV2 5073600507360001
010110010001ETV2 5073700508080001
010110010003ETV2 5073600507360001
010110010003ETV2 5073700508080001
010110010004ETV2 5073600507360001
010110010004ETV2 5073700508080001
010110010004ETV2 5094100509410001
010110010004GEMI 5111200511330001
now the first 10 digit is homeid next 2 is member id ie
0101100100 is homeid and 01 is memeber id and next is channelname and channelname cannot be more than 5 letters.
now i want to compare these homeid in such a way so that i can get the count of unique channels that are watched by these homeids in a new text file in the format
0101100100 3 where 010110010 is homeid and 3 is the count of unique channels that is watched by these id,so on for every id.
any help or idea how could i do it.sir i am not that at programming but i m still trying,i sincerely thank u for ur help.:)
I'd suggest that to simplify things, rather than setting up two arrays (for homeid and channel) you just go with one. Here’s roughly how I think I would do it...
Read your file, and on each line you pull out the homeid and channel to a string (skip over the member id) by using Mid() function (and I suppose the Instr() function if the channel name can vary in length). So for example, after reading the first line you end up with this string: "0101100100DD8"
Once you have this string, you loop through all the entries in your array until you find a match. If you don’t find one, add it to the end.
Keep on doing this until you hit the end of the file.
Once you have finished reading the file, your array will contain a complete list of all the unique homeid/channel combinations. (To move forward, you will probably want to sort them into order. If you’re not sure how to do that, try a bit of a search for VB sorting algorithms – it’s really quite simple. There are all sorts of sophisticated methods of sorting, but the simplest is just to keep looping through the array and swapping things that aren’t the right way around, until there aren’t any to swap. Otherwise known as the “bubble sort”.)
Assuming your array is in an ordered sequence, you can just do a FOR loop from start to finish. Add up the number of entries you find for each homeid, and you have your result.
I think this logic will produce the result you want. We’re discouraged from providing complete code solutions on TheScripts these days, as we want to try and ensure that people think about things and put in some real work themselves, rather than simply doing a copy/paste operation.
I'd suggest that to simplify things, rather than setting up two arrays (for homeid and channel) you just go with one. Here’s roughly how I think I would do it...
Read your file, and on each line you pull out the homeid and channel to a string (skip over the member id) by using Mid() function (and I suppose the Instr() function if the channel name can vary in length). So for example, after reading the first line you end up with this string: "0101100100DD8"
Once you have this string, you loop through all the entries in your array until you find a match. If you don’t find one, add it to the end.
Keep on doing this until you hit the end of the file.
Once you have finished reading the file, your array will contain a complete list of all the unique homeid/channel combinations. (To move forward, you will probably want to sort them into order. If you’re not sure how to do that, try a bit of a search for VB sorting algorithms – it’s really quite simple. There are all sorts of sophisticated methods of sorting, but the simplest is just to keep looping through the array and swapping things that aren’t the right way around, until there aren’t any to swap. Otherwise known as the “bubble sort”.)
Assuming your array is in an ordered sequence, you can just do a FOR loop from start to finish. Add up the number of entries you find for each homeid, and you have your result.
I think this logic will produce the result you want. We’re discouraged from providing complete code solutions on TheScripts these days, as we want to try and ensure that people think about things and put in some real work themselves, rather than simply doing a copy/paste operation.
Hey that was really cool ,I really got the logic how to do it and thanks a ton for ur help
Hey that was really cool ,I really got the logic how to do it and thanks a ton for ur help
Glad to help. :)
It's a lot more satisfying for me, and helpful for you, if I can help you to understand your process rather than just posting a bunch of code for you to copy.
Glad to help. :)
It's a lot more satisfying for me, and helpful for you, if I can help you to understand your process rather than just posting a bunch of code for you to copy.
Hello sir,
I am writing it again,but as you have said i did the same thing,but my mam told
me that the logic is wrong and u need to check the logic.
so sir i m giving the code again and if u can modify the code it will be very helpful.
now as per my description we need to have the count of Unique channel that are being watched by different homeid in a new textfile something like this suppose the homeid 0101100100 has watched 20 unique channels then
output file should contain 0101100100 20.
now sir as per your logic I wrote the program and the results that i m able to generate is that i have the unique channelname that are being watched by this homeid something like this
0101100100GEMI
0101100100SITMU
0101100100DD8
0101100100ENAD
0101100100ETV2
0101100100GEMI
0101100100SITMU
0101100300DISNY
0101100300ENAD
0101100300GEMI
0101100300MAATV
0101100300NDT24
but we want the count of all unique channels watched by a particular homeid in a particular format
0101100100 8
0101100300 9 and so on
sir pls help me here is the code that i have written -
-
-
Option Explicit
-
Dim fsys As New FileSystemObject
-
Dim txtstream As TextStream
-
Dim outstream As TextStream
-
-
-
Private Sub cmdclick_Click()
-
Dim j As Integer
-
Dim searchline As String
-
Dim filename As String
-
Dim homeid As String
-
Dim prevhomeid As String
-
Dim channelname As String
-
Dim channelcount As Integer
-
Dim channelarray(0 To 100000) As String
-
Dim newchannelarray(0 To 100000) As String
-
Dim carray As String
-
Dim prevcarray As String
-
Dim i As Double
-
-
-
prevcarray = ""
-
For j = 0 To lstmarket.ListCount - 1
-
If lstmarket.Selected(j) = True Then
-
-
-
If Len(Trim(txtyear.Text)) > 2 Or IsNumeric(Trim(txtyear.Text)) = False Then
-
MsgBox "this should be 2 digit number "
-
txtyear.Text = ""
-
txtyear.SetFocus
-
Exit Sub
-
End If
-
-
If Trim(txtweek) <= 9 And Len(Trim(txtweek)) < 2 Or IsNumeric(txtweek.Text) = False Then
-
txtweek = "0" & Trim(txtweek)
-
End If
-
-
If Trim(txtday) > 7 Or IsNumeric(txtday) = False Then
-
MsgBox "Please enter number which is equal to or less than 7"
-
txtday.Text = ""
-
txtday.SetFocus
-
Exit Sub
-
End If
-
-
filename = lstmarket.List(j) & Trim(txtyear) & Trim(txtweek) & Trim(txtday) & ".swd"
-
MsgBox ("you have selected the following" & filename)
-
End If
-
Next j
-
-
'********main code for listing all the channels that the house id watch*******
-
-
If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
-
MsgBox "file is open"
-
Else
-
MsgBox "file not found"
-
End If
-
'prevcarray = ""
-
'prevchannelname = ""
-
'prevhomeid = ""
-
channelcount = 0
-
'prevchannelarray(k) = ""
-
Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
-
Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
-
Do Until txtstream.AtEndOfStream
-
searchline = txtstream.ReadLine
-
homeid = Mid(searchline, 1, 10)
-
channelname = Mid(searchline, 13, 5)
-
channelarray(i) = homeid & channelname
-
- prev time u said that i have to use the for loop to check whether each homeid and channelname is there in the or not.
-
sir but i m not able to use the for loop to check in the array,i think
-
If channelarray(i) <> channelarray(i + 1) Then
-
outstream.WriteLine (channelarray(i))
-
End If
-
channelarray(i + 1) = channelarray(i)
-
Loop
-
MsgBox "process completed"
-
-
End Sub
-
-
sir pls help.
Well, you certainly do have some interesting logic there.
I don't have a lot of time right now, so I've banged together a modified version that I hope will help you with the process. I have not tested it, as I don't have any test data and can't spare the time to create some. Anyway, after making sure you've got a backup copy of your current code (in case mine makes things worse) just replace the contents of the cmdclick_Click routine with this, and see how it goes... - Dim j As Integer
-
Dim searchline As String
-
Dim filename As String
-
Dim homeid As String
-
Dim prevhomeid As String
-
Dim channelname As String
-
Dim channelcount As Integer
-
Dim HomeAndChannel As String ' <-- Added this variable.
-
Dim Exists As Boolean ' <-- And this one.
-
Dim ChannelsToReport As Long ' <-- This one, too.
-
Dim channelarray(0 To 100000) As String
-
' Dim newchannelarray(0 To 100000) As String
-
' Dim carray As String
-
' Dim prevcarray As String
-
'Dim i As Double
-
Dim i As Long
-
-
' prevcarray = ""
-
For j = 0 To lstMarket.ListCount - 1
-
If lstMarket.Selected(j) = True Then
-
If Len(Trim(txtYear.Text)) > 2 Or IsNumeric(Trim(txtYear.Text)) = False Then
-
MsgBox "This should be 2 digit number "
-
txtYear.Text = ""
-
txtYear.SetFocus
-
Exit Sub
-
End If
-
-
If Trim(txtWeek) <= 9 And Len(Trim(txtWeek)) < 2 Or (IsNumeric(txtWeek.Text) = False) Then
-
txtWeek = "0" & Trim(txtWeek)
-
End If
-
-
If Trim(txtDay) > 7 Or (IsNumeric(txtDay) = False) Then
-
MsgBox "Please enter number which is equal to or less than 7"
-
txtDay.Text = ""
-
txtDay.SetFocus
-
Exit Sub
-
End If
-
-
filename = lstMarket.List(j) & Trim(txtYear) & Trim(txtWeek) & Trim(txtDay) & ".swd"
-
MsgBox ("you have selected the following : " & filename)
-
End If
-
Next j
-
-
'********main code for listing all the channels that the house id watch*******
-
-
If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
-
MsgBox "file is open"
-
Else
-
MsgBox "file not found"
-
Exit Sub
-
End If
-
'prevcarray = ""
-
'prevchannelname = ""
-
'prevhomeid = ""
-
channelcount = 0
-
'prevchannelarray(k) = ""
-
Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
-
Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
-
-
-
-
' ****************************************
-
' Step 1: Build an array of all unique homeid/channel combinations
-
' ****************************************
-
-
Do Until txtstream.AtEndOfStream
-
searchline = txtstream.ReadLine
-
homeid = Mid(searchline, 1, 10)
-
channelname = Mid(searchline, 13, 5)
-
'channelarray(i) = homeid & channelname
-
HomeAndChannel = homeid & channelname
-
-
-
' Check whether we have already seen this combination...
-
Exists = False
-
For i = 1 To channelcount
-
If channelarray(i) = HomeAndChannel Then
-
Exists = True
-
Exit For
-
End If
-
Next
-
-
' If not, add it to the array.
-
If Not Exists Then
-
channelcount = channelcount + 1
-
channelarray(channelcount) = HomeAndChannel
-
End If
-
Loop
-
-
-
-
' ****************************************
-
' Step 2: Sort the array. I'll leave this to you, as I'm short on time
-
' ****************************************
-
-
-
-
-
-
' ****************************************
-
' Step 3: Scan the array and report the number of channels per homeid
-
' ****************************************
-
-
ChannelsToReport = 1
-
prevhomeid = Left$(channelarray(1), 10)
-
For i = 2 To channelcount
-
homeid = Left$(channelarray(i), 10)
-
If homeid = prevhomeid Then
-
ChannelsToReport = ChannelsToReport + 1
-
Else
-
outstream.WriteLine prevhomeid & " " & Format(ChannelsToReport)
-
ChannelsToReport = 1
-
prevhomeid = homeid
-
End If
-
Next
-
MsgBox "process completed"
Well, you certainly do have some interesting logic there.
I don't have a lot of time right now, so I've banged together a modified version that I hope will help you with the process. I have not tested it, as I don't have any test data and can't spare the time to create some. Anyway, after making sure you've got a backup copy of your current code (in case mine makes things worse) just replace the contents of the cmdclick_Click routine with this, and see how it goes... - Dim j As Integer
-
Dim searchline As String
-
Dim filename As String
-
Dim homeid As String
-
Dim prevhomeid As String
-
Dim channelname As String
-
Dim channelcount As Integer
-
Dim HomeAndChannel As String ' <-- Added this variable.
-
Dim Exists As Boolean ' <-- And this one.
-
Dim ChannelsToReport As Long ' <-- This one, too.
-
Dim channelarray(0 To 100000) As String
-
' Dim newchannelarray(0 To 100000) As String
-
' Dim carray As String
-
' Dim prevcarray As String
-
'Dim i As Double
-
Dim i As Long
-
-
' prevcarray = ""
-
For j = 0 To lstMarket.ListCount - 1
-
If lstMarket.Selected(j) = True Then
-
If Len(Trim(txtYear.Text)) > 2 Or IsNumeric(Trim(txtYear.Text)) = False Then
-
MsgBox "This should be 2 digit number "
-
txtYear.Text = ""
-
txtYear.SetFocus
-
Exit Sub
-
End If
-
-
If Trim(txtWeek) <= 9 And Len(Trim(txtWeek)) < 2 Or (IsNumeric(txtWeek.Text) = False) Then
-
txtWeek = "0" & Trim(txtWeek)
-
End If
-
-
If Trim(txtDay) > 7 Or (IsNumeric(txtDay) = False) Then
-
MsgBox "Please enter number which is equal to or less than 7"
-
txtDay.Text = ""
-
txtDay.SetFocus
-
Exit Sub
-
End If
-
-
filename = lstMarket.List(j) & Trim(txtYear) & Trim(txtWeek) & Trim(txtDay) & ".swd"
-
MsgBox ("you have selected the following : " & filename)
-
End If
-
Next j
-
-
'********main code for listing all the channels that the house id watch*******
-
-
If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
-
MsgBox "file is open"
-
Else
-
MsgBox "file not found"
-
Exit Sub
-
End If
-
'prevcarray = ""
-
'prevchannelname = ""
-
'prevhomeid = ""
-
channelcount = 0
-
'prevchannelarray(k) = ""
-
Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
-
Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
-
-
-
-
' ****************************************
-
' Step 1: Build an array of all unique homeid/channel combinations
-
' ****************************************
-
-
Do Until txtstream.AtEndOfStream
-
searchline = txtstream.ReadLine
-
homeid = Mid(searchline, 1, 10)
-
channelname = Mid(searchline, 13, 5)
-
'channelarray(i) = homeid & channelname
-
HomeAndChannel = homeid & channelname
-
-
-
' Check whether we have already seen this combination...
-
Exists = False
-
For i = 1 To channelcount
-
If channelarray(i) = HomeAndChannel Then
-
Exists = True
-
Exit For
-
End If
-
Next
-
-
' If not, add it to the array.
-
If Not Exists Then
-
channelcount = channelcount + 1
-
channelarray(channelcount) = HomeAndChannel
-
End If
-
Loop
-
-
-
-
' ****************************************
-
' Step 2: Sort the array. I'll leave this to you, as I'm short on time
-
' ****************************************
-
-
-
-
-
-
' ****************************************
-
' Step 3: Scan the array and report the number of channels per homeid
-
' ****************************************
-
-
ChannelsToReport = 1
-
prevhomeid = Left$(channelarray(1), 10)
-
For i = 2 To channelcount
-
homeid = Left$(channelarray(i), 10)
-
If homeid = prevhomeid Then
-
ChannelsToReport = ChannelsToReport + 1
-
Else
-
outstream.WriteLine prevhomeid & " " & Format(ChannelsToReport)
-
ChannelsToReport = 1
-
prevhomeid = homeid
-
End If
-
Next
-
MsgBox "process completed"
Thanks for your prompt response sir
Although it was very helpful i did not have the desired results.
now according to u i have to sort the array in step 2 here is how i am doing it using bubble sort ,but it is giving me an error.
Step 2 to sort array -
'sorting of array
-
For i = LBound(channelarray) To UBound(channelarray) - 1
-
For k = i + 1 To Ubound(channelarray) - 1 <-------- this step gives error overflow.
-
If channelarray(i) > channelarray(k) Then
-
tmpsort = channelarray(i)
-
channelarray(i) = channelarray(k)
-
channelarray(k) = tmpsort
-
End If
-
Next
-
Next
-
As per the output if i dont sort it the output has something like this
0101100300 1
0101100100 4
0101100300 1
0101100100 4
0101100300 1
0101100100 4
0101100300 11
0101100400 1
0101100100 4
0101100300 11
0101100400 1
0101100100 4
although some count are right some are wrong,it is giving me the count of homeid suppose if homeid has repeated 11 time then it gives
0101100300 11
as u can see sir,anyways sir pls tell why array sort is giving me error.
and what changes should i make.
Thank u sir for quick response ,effort and time .
The first thing I would do is move the sort out to a separate Sub to avoid interfering with your existing processing. That also gives you a generic sort routine that you can use for other things.
One question - what is k defined as? If it's Integer, it can only go up to 32768, or somewhere around there. I normally use Long, for that reason and also for reasons of performance (it's the "native" data type on 32-bit processors so you save time on conversions).
(I've just started work. Won't have time to look at your code until lunchtime, sorry.)
The first thing I would do is move the sort out to a separate Sub to avoid interfering with your existing processing. That also gives you a generic sort routine that you can use for other things.
One question - what is k defined as? If it's Integer, it can only go up to 32768, or somewhere around there. I normally use Long, for that reason and also for reasons of performance (it's the "native" data type on 32-bit processors so you save time on conversions).
(I've just started work. Won't have time to look at your code until lunchtime, sorry.)
Oh no problem sir, One thing i would like to mention u about your code is that
it gives the results but suppose if my textfiles contains 98873 lines of data.
the output file has around over 2 lakhs lines of data. and the output file
keeps on repeating the homeid again and again ,from this i mean that
suppose homeid 0007100200 has watched 1 unique channels
this is repeated quite a number of times,why is this happening.
Oh no problem sir, One thing i would like to mention u about your code is that
it gives the results but suppose if my textfiles contains 98873 lines of data.
the output file has around over 2 lakhs lines of data. and the output file
keeps on repeating the homeid again and again ,from this i mean that
suppose homeid 0007100200 has watched 1 unique channels
this is repeated quite a number of times,why is this happening.
Good question.
I'm home now, so I can take a bit of time to go over the code. Stay tuned...
Um... what's a lakh?
Ok, I think there's a bug in your sort.
The code looks good (did you manage to resolve the overflow error?) but I think you need to make one small change. You have the right idea, but I think you are only causing things to "bubble up" part of the way. Try this... - Change this line...
-
For k = i + 1 To Ubound(channelarray) - 1
-
to...
-
For k = Ubound(channelarray) - 1 To i Step -1
I'm not going to look at the rest of the code just yet, because I think that fixing the sort may resolve your problem.
Ok, I think there's a bug in your sort.
The code looks good (did you manage to resolve the overflow error?) but I think you need to make one small change. You have the right idea, but I think you are only causing things to "bubble up" part of the way. Try this... - Change this line...
-
For k = i + 1 To Ubound(channelarray) - 1
-
to...
-
For k = Ubound(channelarray) - 1 To i Step -1
I'm not going to look at the rest of the code just yet, because I think that fixing the sort may resolve your problem.
i mean that the text file has only around 9000 lines of data and the output txtfile that is generated is over 1lakh lines of data. i mean homeid keeps on repeating and repeating.
i mean that the text file has only around 9000 lines of data and the output txtfile that is generated is over 1lakh lines of data. i mean homeid keeps on repeating and repeating.
sir once i write the sorting array code tha entire project just hangs .why does this happen
sir once i write the sorting array code tha entire project just hangs .why does this happen
Without seeing the whole of the code, it's difficult to say. But probably you (or I) have managed to create an infinite loop there somewhere.
Did you move the sort out to a separate Sub and pass the array to it? I think that will be a lot safer. Modular code like that is also much easier to debug.
And I still don't know what "1lakh" means.
Without seeing the whole of the code, it's difficult to say. But probably you (or I) have managed to create an infinite loop there somewhere.
Did you move the sort out to a separate Sub and pass the array to it? I think that will be a lot safer. Modular code like that is also much easier to debug.
And I still don't know what "1lakh" means.
sir is it possible that i can send u a sample text file so that u can get a clear
picture of what i m talkin about.in this way u will have the sample txtfile to test or else sir if u want i can send the entire project.and yes sir i tried to do that
but was very unsucessful.
Without seeing the whole of the code, it's difficult to say. But probably you (or I) have managed to create an infinite loop there somewhere.
Did you move the sort out to a separate Sub and pass the array to it? I think that will be a lot safer. Modular code like that is also much easier to debug.
And I still don't know what "1lakh" means.
and sir I m sending the entire code again in the way i m doing it,hope it helps -
-
Option Explicit
-
Dim fsys As New FileSystemObject
-
Dim txtstream As TextStream
-
Dim outstream As TextStream
-
-
-
-
Public Sub cmdclick_Click()
-
Dim j As Integer
-
Dim searchline As String
-
Dim filename As String
-
Dim homeid As String
-
Dim prevhomeid As String
-
Dim channelname As String
-
Dim channelcount As Integer
-
Dim channelarray(0 To 100000) As String
-
Dim HomeAndChannel As String
-
Dim Exists As Boolean
-
Dim ChannelsToReport As Long
-
Dim carray As String
-
Dim prevcarray As String
-
Dim i As Double
-
Dim k As Double
-
Dim tmpsort As String
-
-
-
-
-
-
prevcarray = ""
-
For j = 0 To lstmarket.ListCount - 1
-
If lstmarket.Selected(j) = True Then
-
-
-
If Len(Trim(txtyear.Text)) > 2 Or IsNumeric(Trim(txtyear.Text)) = False Then
-
MsgBox "this should be 2 digit number "
-
txtyear.Text = ""
-
txtyear.SetFocus
-
Exit Sub
-
End If
-
-
If Trim(txtweek) <= 9 And Len(Trim(txtweek)) < 2 Or IsNumeric(txtweek.Text) = False Then
-
txtweek = "0" & Trim(txtweek)
-
End If
-
-
If Trim(txtday) > 7 Or IsNumeric(txtday) = False Then
-
MsgBox "Please enter number which is equal to or less than 7"
-
txtday.Text = ""
-
txtday.SetFocus
-
Exit Sub
-
End If
-
-
filename = lstmarket.List(j) & Trim(txtyear) & Trim(txtweek) & Trim(txtday) & ".swd"
-
MsgBox ("you have selected the following" & filename)
-
End If
-
Next j
-
-
'********main code for listing all the channels that the house id watch*******
-
-
If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
-
MsgBox "file is open"
-
Else
-
MsgBox "file not found"
-
End If
-
channelcount = 0
-
Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
-
Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
-
Do Until txtstream.AtEndOfStream
-
searchline = txtstream.ReadLine
-
homeid = Mid(searchline, 1, 10)
-
channelname = Mid(searchline, 13, 5)
-
channelarray(i) = homeid & channelname
-
HomeAndChannel = homeid & channelname
-
Exists = False
-
For i = 0 To channelcount
-
If channelarray(i) = HomeAndChannel Then
-
Exists = True
-
Exit For
-
End If
-
-
Next
-
-
If Not Exists Then
-
channelcount = channelcount + 1
-
channelarray(channelcount) = HomeAndChannel
-
-
Loop
-
-
'sorting array<----------- code for sorting
-
-
For i = LBound(channelarray) To UBound(channelarray) - 1
-
For k = UBound(channelarray) - 1 To i Step -1
-
If channelarray(i) > channelarray(k) Then
-
tmpsort = channelarray(k)
-
channelarray(k) = channelarray(i)
-
channelarray(i) = tmpsort
-
End If
-
Next
-
Next
-
-
-
-
ChannelsToReport = 1
-
prevhomeid = Left$(channelarray(i), 10)
-
For i = 1 To channelcount
-
homeid = Left$(channelarray(i), 10)
-
If homeid = prevhomeid Then
-
ChannelsToReport = ChannelsToReport + 1
-
Else
-
outstream.WriteLine prevhomeid & " " & Format(ChannelsToReport)
-
ChannelsToReport = 1
-
prevhomeid = homeid
-
-
End If
-
Next
-
End If
-
-
'Loop<---am i right in putting this loop here
-
MsgBox "process completed"
-
-
End Sub
-
sir 1 lakh is the count of number of lines that is produced after the execution of
the program,it is surprising bcoz the file that i am using as inpu has aroun 9884
homeids so output can never be that large.
Without seeing the whole of the code, it's difficult to say. But probably you (or I) have managed to create an infinite loop there somewhere.
Did you move the sort out to a separate Sub and pass the array to it? I think that will be a lot safer. Modular code like that is also much easier to debug.
And I still don't know what "1lakh" means.
Here is the output tht it creates
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100100 5
0101100300 11
0101100100 5
0101100300 11
0101100100 5
0101100300 11
0101100100 5
0101100300 11
0101100100 5
0101100300 11
0101100100 5
0101100300 11
0101100100 5
0101100300 11
0101100100 5
0101100300 11
0101100100 5
0101100300 11
0101100100 5
0101100300 11
0101100100 5
0101100300 11
it creates redundant rows in the output file.
and hence the size of output file increases.
sir 1 lakh is the count of number of lines that is produced after the execution of the program,it is surprising bcoz the file that i am using as inpu has aroun 9884 homeids so output can never be that large.
Ok, I finally managed to track down "lakh" via Google. So, it's a hundred thousand, huh? Where is this term used? I've never heard it before.
That code does not compile. There's a block IF without an END IF, just before the sorting code. So this can't be the code you are executing.
I still think that the sorting is probably behind any further problems. So in my opinion, you should ignore (or even comment out) the subsequent code and concentrate on the sort to make sure it works, before moving on. I'd suggest you cut down one of your data files to ... oh, say a hundred records. Then read that in, see what is in your array, then run it through the sort routine and see what comes out.
Also, a couple of observations on the code... - I'd recommend reducing the indenting, and tidying it up so it makes a bit more sense. The idea is to indent things to show how they are logically nested within other structures. You appear to have indented each section of code further than the last, so that it all goes way off the side and one has to scroll over to see it.
- When building the array, I used
For i = 1 To channelcount
While you used For i = 0 To channelcount
I’m not sure how significant the difference is, but in programming you really have to watch the little details, or they’ll bite you. - I commented out the line
channelarray(i) = homeid & channelname
while you still have it there. I’m not sure how significant this is, since i is zero at this point. - I think I found your missing End If, down the bottom just above the MsgBox statement.
you could use selectunique() and specify your filename
then it would return and array of DataEntry to write it to file call WriteData() and specify
you new filename and Dataentry Array
as you can see there no error handler, so its up to you to add these feature
[HTML]
publictype Dataentry
HomeID as string
UniqueChannels as long
channels() as string 'you could put * 5 to limit character
end type
public function selectuniques(byval filename as string) as DataEntry()
dim retval() as string 'temporary variable for return
dim temp as string 'temporary variable for line read
dim fr as int 'file pointer
fr = freefile() 'get free file pointer
open filename for input as fr 'open file(input means read)
while not Eof(fr) 'loop until end of file
input line fr,temp 'read line
PushData(mid(temp,1,10) ,mid(temp,12,5),retal()) 'Call the Pushdata - which checks and add uniques
wend
close fr 'close the file
selectuniques = retval()
end sub
public sub WriteData(byval filename as string,_entries as DataEntry())
dim fr as int
dim temp as string
fr = freefile()
open filename for output as fr
for i = 0 to ubound(_entries)
temp = _entries(i).Homeid & " " & _entries(i).uniquechannels
print fr,temp
next
close fr
end sub
private sub PushData(byval _homeId as string,_Channelname as string,byref _entrylist as DataEntry())
dim found as boolean,channelfound as boolean
dim _index as long
found =false
channelfound =false
if ubound(_entrylist)=0 then 'check where _entrylist where empty array
redim _entrylist(1) as DataEntry
_entrylist(0).HomeId = _homeId
redim _entrylist(0).Channels(1) as string
_entrylist(0).channels(0) = _channelname
_entrylist(0).Uniquechannel = 1
else
for i = 0 to ubound(_entrylist) 'loop throughout the entries
if _newentry = _entrylist(i).homeid then 'check whether the homeid exist
if ubound(_entrylist(i).channels) = 0 then 'check whether the channel list where empty, if empty add new channel
redim Preserve _entrylist(i).channels(1) as string
_entrylist(i).uniquechannnels = 1
_entrylist(i).channels(0) = _channelname
else
for j=0 to ubound(_entrylist(i).channels)
if _entrylist(i).channels(j) = _channelname then
channelfound =true
exit for
end if
next
end if
_index = i
found = true
Exit for 'if home id were found then exit the for loop
end if
next
if not found then 'when home id was not found then add it to DataEntry array
redim preserve _entrylist(ubound(_entrylist)+1) as DataEntry
_entrylist(ubound(_entrylist).Homeid = _homeid
_entrylist(ubound(_entrylist).uniquechannels=_entr ylist(ubound(_entrylist).uniquechannels + 1 'increment unique
'
redim Preserve _entrylist(ubound(_entrylist)).channels(1) as string
_entrylist(ubound(_entrylist)).channels(0) = _channelname
elseif not channelfound then
redim preserver _entrylist(i).channels(ubound(_entrylist(i).channe ls)+1) as string
_entrylist(i).uniquechannels = _entrylist(i).uniquechannels + 1
_entrylist(i).channels(ubound(_entrylist(i).channe ls)) = _channelname
end if
end if
end sub
[/HTML]
by the way if you will copy it to a form just change the type to private
or
just put in on a module(most efficient)
just email me @ Removed by Moderator if you find these suck or you want a breif explanation
by the way i havent test that code... because i dont have a vb6 in my computer right now, cos i already migrated to other language like C# .
you know, it would have been easier if you transfer that textfile to a temporary database file like MS Access and use queries to get your desired output.
you're output is simple. And and SQL script using DISTINCT would do the trick. It seems you've been using the long method.
but if you have a device that log it to a text file
database has no use if you want to directly query the textfile
database are only used for hierarchical data but these type operation
doesnt require more than a plain text format.
and besides why use temporary database if you could use an array...
array as faster than temporary database like access..
yep... you might be right about speed of processing... by how much an hour? but if you're developing and you have a deadline. do what is the fastest right? :D
it's just a suggestion. sometimes we trade processing time, for faster output and flexibility. and what if you have a new requirement for you report? then what? :D redo the program again? that's what i'm saying... if the SCRIPT can do it... then it'll be faster in the long run... in terms of producing it.
cheers.
...just email me @ Removed by Moderator if you find these suck or you want a breif explanation...
Sorry, but I've erased your e-mail address. Please don't post e-mail addresses here, as forums like this are often scanned by spammers and scammers to pick up any e-mail addresses they can find.
You have two real options... - Just ask people to click on your ID, and from your profile they can hit the "send a message via email" link (if you have it enabled), or
- Obfuscate the address so that a person reading the post can see what you mean and translate it, but an automated scan is likely to miss it.
TheScripts has a policy of discouraging the posting of e-mail addresses - see the FAQ.
yep... you might be right about speed of processing... by how much an hour? but if you're developing and you have a deadline. do what is the fastest right? :D
Personally, I would consider the use of a database to be serious overkill in this case, and only to be used as a last resort. The task is actually quite a simple one, which can be accomplished by merely reading in the text file and writing out the result. Why add all the complexity and overheads of a database?
I'm on lunch now, so I think I'll have a go at producing a fully working version. Stay tuned...
Personally, I would consider the use of a database to be serious overkill in this case, and only to be used as a last resort. The task is actually quite a simple one, which can be accomplished by merely reading in the text file and writing out the result. Why add all the complexity and overheads of a database?
I'm on lunch now, so I think I'll have a go at producing a fully working version. Stay tuned...
Here you go, I believe this is a fully working version. At least it worked with a small data sample that I pulled from your earlier posts.
A couple of things to keep in mind. - To keep things simple, I've commented out the entire section which validates your selection and builds the file name. I just assumed a file called "sample.swd" in the same directory as the project.
- Results are written to "channelCount.txt" in the same directory.
- There's no error checking (of course).
- This is written assuming that the homeid is the first 10 characters of each record - I seem to recall there was some question over whether it should be 10 or 12.
- It might be more efficient to generate the count as you build the array. Each time a new entry is added to the array of unique HomeId/Channel combinations, you could increment a count for that HomeId (in another array). This would remove the need to run through the array later and count them.
- Private Sub cmdclick_Click()
-
-
Dim i As Long ' General loop counter.
-
Dim j As Integer ' General loop counter.
-
Dim FileName As String ' File to be scanned.
-
Dim SearchLine As String ' Input line from data file.
-
-
' Used in building and sorting array of unique homeid/channel combinations...
-
Dim Exists As Boolean
-
Dim HomeAndChannel As String
-
Dim ChannelArray(0 To 100000) As String
-
Dim ArrayCount As Long ' Number of entries placed in the array.
-
-
' Used in counting the unique channels per homeid...
-
Dim HomeId As String
-
Dim PrevHomeId As String
-
Dim ChannelCount As Integer
-
-
-
' prevcarray = ""
-
' For j = 0 To lstMarket.ListCount - 1
-
' If lstMarket.Selected(j) = True Then
-
' If Len(Trim(txtYear.Text)) > 2 Or IsNumeric(Trim(txtYear.Text)) = False Then
-
' MsgBox "This should be 2 digit number "
-
' txtYear.Text = ""
-
' txtYear.SetFocus
-
' Exit Sub
-
' End If
-
'
-
' If Trim(txtWeek) <= 9 And Len(Trim(txtWeek)) < 2 Or (IsNumeric(txtWeek.Text) = False) Then
-
' txtWeek = "0" & Trim(txtWeek)
-
' End If
-
'
-
' If Trim(txtDay) > 7 Or (IsNumeric(txtDay) = False) Then
-
' MsgBox "Please enter number which is equal to or less than 7"
-
' txtDay.Text = ""
-
' txtDay.SetFocus
-
' Exit Sub
-
' End If
-
'
-
' filename = lstMarket.List(j) & Trim(txtYear) & Trim(txtWeek) & Trim(txtDay) & ".swd"
-
' MsgBox ("you have selected the following : " & filename)
-
' End If
-
' Next j
-
-
FileName = App.Path & "\Sample.swd"
-
-
' ********main code for listing all the channels that the house id watch*******
-
-
If fsys.FileExists(FileName) Then
-
MsgBox "file is open"
-
Else
-
MsgBox "file not found"
-
Exit Sub
-
End If
-
-
Set txtstream = fsys.OpenTextFile(FileName, ForReading, False)
-
Set outstream = fsys.OpenTextFile(App.Path & "\ChannelCount.txt", ForWriting, True)
-
-
-
-
' ****************************************
-
' Step 1: Build an array of all unique homeid/channel combinations
-
' ****************************************
-
-
ArrayCount = 0
-
Do Until txtstream.AtEndOfStream
-
SearchLine = txtstream.ReadLine
-
HomeAndChannel = Left(SearchLine, 10) & Mid(SearchLine, 13, 5)
-
' Check whether we have already seen this combination.
-
Exists = False
-
For i = 1 To ArrayCount
-
If ChannelArray(i) = HomeAndChannel Then
-
Exists = True
-
Exit For
-
End If
-
Next
-
' If not, add it to the array.
-
If Not Exists Then
-
ArrayCount = ArrayCount + 1
-
ChannelArray(ArrayCount) = HomeAndChannel
-
End If
-
Loop
-
-
-
-
' ****************************************
-
' Step 2: Sort the array.
-
' ****************************************
-
-
Dim k As Long
-
Dim tmpsort As String
-
-
For i = 1 To ArrayCount - 1
-
For j = ArrayCount - 1 To i Step -1
-
k = j + 1
-
If ChannelArray(j) > ChannelArray(k) Then
-
tmpsort = ChannelArray(j)
-
ChannelArray(j) = ChannelArray(k)
-
ChannelArray(k) = tmpsort
-
End If
-
Next
-
Next
-
-
-
-
' ****************************************
-
' Step 3: Scan the array and report the number of channels per homeid
-
' ****************************************
-
-
ChannelCount = 1
-
PrevHomeId = Left$(ChannelArray(1), 10)
-
For i = 2 To ArrayCount
-
HomeId = Left$(ChannelArray(i), 10)
-
If HomeId = PrevHomeId Then
-
ChannelCount = ChannelCount + 1
-
Else
-
outstream.WriteLine PrevHomeId & " " & Format(ChannelCount)
-
ChannelCount = 1
-
PrevHomeId = HomeId
-
End If
-
Next
-
' Write out the last entry, which will otherwise be missed.
-
outstream.WriteLine PrevHomeId & " " & Format(ChannelCount)
-
outstream.Close
-
txtstream.Close
-
-
MsgBox "process completed"
-
-
End Sub
Here you go, I believe this is a fully working version. At least it worked with a small data sample that I pulled from your earlier posts.
A couple of things to keep in mind.- To keep things simple, I've commented out the entire section which validates your selection and builds the file name. I just assumed a file called "sample.swd" in the same directory as the project.
- Results are written to "channelCount.txt" in the same directory.
- There's no error checking (of course).
- This is written assuming that the homeid is the first 10 characters of each record - I seem to recall there was some question over whether it should be 10 or 12.
- It might be more efficient to generate the count as you build the array. Each time a new entry is added to the array of unique HomeId/Channel combinations, you could increment a count for that HomeId (in another array). This would remove the need to run through the array later and count them.
- Private Sub cmdclick_Click()
-
-
Dim i As Long ' General loop counter.
-
Dim j As Integer ' General loop counter.
-
Dim FileName As String ' File to be scanned.
-
Dim SearchLine As String ' Input line from data file.
-
-
' Used in building and sorting array of unique homeid/channel combinations...
-
Dim Exists As Boolean
-
Dim HomeAndChannel As String
-
Dim ChannelArray(0 To 100000) As String
-
Dim ArrayCount As Long ' Number of entries placed in the array.
-
-
' Used in counting the unique channels per homeid...
-
Dim HomeId As String
-
Dim PrevHomeId As String
-
Dim ChannelCount As Integer
-
-
-
' prevcarray = ""
-
' For j = 0 To lstMarket.ListCount - 1
-
' If lstMarket.Selected(j) = True Then
-
' If Len(Trim(txtYear.Text)) > 2 Or IsNumeric(Trim(txtYear.Text)) = False Then
-
' MsgBox "This should be 2 digit number "
-
' txtYear.Text = ""
-
' txtYear.SetFocus
-
' Exit Sub
-
' End If
-
'
-
' If Trim(txtWeek) <= 9 And Len(Trim(txtWeek)) < 2 Or (IsNumeric(txtWeek.Text) = False) Then
-
' txtWeek = "0" & Trim(txtWeek)
-
' End If
-
'
-
' If Trim(txtDay) > 7 Or (IsNumeric(txtDay) = False) Then
-
' MsgBox "Please enter number which is equal to or less than 7"
-
' txtDay.Text = ""
-
' txtDay.SetFocus
-
' Exit Sub
-
' End If
-
'
-
' filename = lstMarket.List(j) & Trim(txtYear) & Trim(txtWeek) & Trim(txtDay) & ".swd"
-
' MsgBox ("you have selected the following : " & filename)
-
' End If
-
' Next j
-
-
FileName = App.Path & "\Sample.swd"
-
-
' ********main code for listing all the channels that the house id watch*******
-
-
If fsys.FileExists(FileName) Then
-
MsgBox "file is open"
-
Else
-
MsgBox "file not found"
-
Exit Sub
-
End If
-
-
Set txtstream = fsys.OpenTextFile(FileName, ForReading, False)
-
Set outstream = fsys.OpenTextFile(App.Path & "\ChannelCount.txt", ForWriting, True)
-
-
-
-
' ****************************************
-
' Step 1: Build an array of all unique homeid/channel combinations
-
' ****************************************
-
-
ArrayCount = 0
-
Do Until txtstream.AtEndOfStream
-
SearchLine = txtstream.ReadLine
-
HomeAndChannel = Left(SearchLine, 10) & Mid(SearchLine, 13, 5)
-
' Check whether we have already seen this combination.
-
Exists = False
-
For i = 1 To ArrayCount
-
If ChannelArray(i) = HomeAndChannel Then
-
Exists = True
-
Exit For
-
End If
-
Next
-
' If not, add it to the array.
-
If Not Exists Then
-
ArrayCount = ArrayCount + 1
-
ChannelArray(ArrayCount) = HomeAndChannel
-
End If
-
Loop
-
-
-
-
' ****************************************
-
' Step 2: Sort the array.
-
' ****************************************
-
-
Dim k As Long
-
Dim tmpsort As String
-
-
For i = 1 To ArrayCount - 1
-
For j = ArrayCount - 1 To i Step -1
-
k = j + 1
-
If ChannelArray(j) > ChannelArray(k) Then
-
tmpsort = ChannelArray(j)
-
ChannelArray(j) = ChannelArray(k)
-
ChannelArray(k) = tmpsort
-
End If
-
Next
-
Next
-
-
-
-
' ****************************************
-
' Step 3: Scan the array and report the number of channels per homeid
-
' ****************************************
-
-
ChannelCount = 1
-
PrevHomeId = Left$(ChannelArray(1), 10)
-
For i = 2 To ArrayCount
-
HomeId = Left$(ChannelArray(i), 10)
-
If HomeId = PrevHomeId Then
-
ChannelCount = ChannelCount + 1
-
Else
-
outstream.WriteLine PrevHomeId & " " & Format(ChannelCount)
-
ChannelCount = 1
-
PrevHomeId = HomeId
-
End If
-
Next
-
' Write out the last entry, which will otherwise be missed.
-
outstream.WriteLine PrevHomeId & " " & Format(ChannelCount)
-
outstream.Close
-
txtstream.Close
-
-
MsgBox "process completed"
-
-
End Sub
Thank you sir for ur time and patience that u put into my project.
sir it is working fine.
Now sir I have another version of this code which produces some what different results for unique channelcount.sir i m sending u the code if u have a look at it it would be wonderful sir. -
-
Private Sub Command1_Click()
-
Dim MemberId As String
-
Dim Channel As String
-
Dim MemberChannel As String
-
Dim MemberChannelArray() As String
-
Dim Cnt As Integer
-
Dim X As String
-
Me.MousePointer = 11
-
Cnt = 0
-
Open App.Path & "\10117055.swd" For Input As #1
-
'This loop will store only one row for a member channel combination in an array
-
Do While Not EOF(1)
-
Line Input #1, X
-
MemberId = left(X, 10)
-
Channel = Mid(X, 13, 5)
-
MemberChannel = MemberId + Channel
-
-
If Cnt = 0 Then
-
Cnt = Cnt + 1
-
ReDim Preserve MemberChannelArray(Cnt)
-
MemberChannelArray(Cnt) = MemberChannel
-
Else
-
If bsearcH(MemberChannelArray, MemberChannel, 1, Cnt) = -1 Then
-
Cnt = Cnt + 1
-
ReDim Preserve MemberChannelArray(Cnt)
-
MemberChannelArray(Cnt) = MemberChannel
-
End If
-
End If
-
Loop
-
Close #1
-
'Sorting the array
-
Call qsorT(MemberChannelArray, 1, Cnt)
-
Dim LastId As String
-
Dim i As Integer
-
Dim MemCnt As Integer
-
MemCnt = 1
-
LastId = left(MemberChannelArray(1), 10)
-
Open App.Path & "\output.txt" For Output As #2
-
For i = 2 To Cnt
-
If LastId <> left(MemberChannelArray(i), 10) Then
-
Print #2, LastId; " "; MemCnt
-
LastId = left(MemberChannelArray(i), 10)
-
MemCnt = 1
-
Else
-
MemCnt = MemCnt + 1
-
End If
-
Next
-
Print #2, LastId; " "; MemCnt
-
Close #2
-
Me.MousePointer = 0
-
End Sub
-
'Function for searching an item in an array using Binary Search Algorithm
-
Private Function bsearcH(lsT() As String, sst As String, ByVal lB As Integer, ByVal uB As Integer) As Integer
-
Dim mD As Integer
-
Do While uB >= lB
-
mD = (uB + lB) / 2
-
If lsT(mD) = sst Then
-
bsearcH = mD
-
Exit Function
-
End If
-
If sst < lsT(mD) Then
-
uB = mD - 1
-
End If
-
If sst > lsT(mD) Then
-
lB = mD + 1
-
End If
-
Loop
-
bsearcH = -1
-
End Function
-
'Subroutine to perform a sort based on Quick Sort Algorithm
-
Public Sub qsorT(in_array() As String, left As Integer, right As Integer)
-
Dim current As Integer, last As Integer
-
If left >= right Then
-
Exit Sub
-
End If
-
Call swaP(in_array, left, (left + right) / 2)
-
-
last = left
-
For current = left + 1 To right
-
If in_array(current) < in_array(left) Then
-
Call swaP(in_array, last, current)
-
End If
-
Next
-
Call swaP(in_array, left, last)
-
Call qsorT(in_array, left, last - 1)
-
Call qsorT(in_array, last + 1, right)
-
-
End Sub
-
-
Private Sub swaP(in_array() As String, i As Integer, j As Integer)
-
Dim temp As String
-
temp = in_array(i)
-
in_array(i) = in_array(j)
-
in_array(j) = temp
-
End Sub
-
now sir is the 2 different output produce by 2 programs for the same inputfile
this is output generated
by the new program
0101100100 5 0101100300 34 <------------------Notice the difference
0101100400 10
0101100500 3
0101100600 10 0101101000 26 <-------------------count over here
this is output generated by the program given by u
0101100100 5 0101100300 11<-----------difference
0101100400 10
0101100500 3
0101100600 10 0101101000 16<-------count over here
It would help if you told us which one is correct.
One quick way would be to open your input file in MS Word, do a Find for "0101100300" and choose "highlight all occurences". This will tell you how many times it finds it.
It would help if you told us which one is correct.
One quick way would be to open your input file in MS Word, do a Find for "0101100300" and choose "highlight all occurences". This will tell you how many times it finds it.
sir the first one is correct the count is 34
It would help if you told us which one is correct.
One quick way would be to open your input file in MS Word, do a Find for "0101100300" and choose "highlight all occurences". This will tell you how many times it finds it.
P.S. A binary search only works if the items are already in sorted sequence. This might explain the difference.
P.S. A binary search only works if the items are already in sorted sequence. This might explain the difference.
sir one thing i would like to mention here is that my inputfile that is the textfile
10117055.txt was already sorted,then why do we require a sorting code.
sir one thing i would like to mention here is that my inputfile that is the textfile
10117055.txt was already sorted,then why do we require a sorting code.
That's a good point. If you know definitely that the data will be sorted already, then obviously you don't need to bother sorting it. Removing the sort will simplify your code, which is a good thing.
I think the problem here is that you are producing a whole new program, then saying "why doesn't it work?". When debugging code you need to learn to break it down and test the individual parts first. For example, do you know whether your quicksort routine works? At what point do your results differ from mine? When the array is built, when it's sorted, or when it is counted up at the end?
It's very important to learn to change one thing at a time, so that if something breaks, you know what is it. Then it becomes much simpler to work out why/how.
(By the way. If you do need to sort, then the QuickSort routine is likely to save you tons of time compared to the simple bubble-type sort we've been using.)
P.S. If the data is already sorted, so you remove the sort and the bug goes away, then that will tell you where it was wrong. :)
That's a good point. If you know definitely that the data will be sorted already, then obviously you don't need to bother sorting it. Removing the sort will simplify your code, which is a good thing.
I think the problem here is that you are producing a whole new program, then saying "why doesn't it work?". When debugging code you need to learn to break it down and test the individual parts first. For example, do you know whether your quicksort routine works? At what point do your results differ from mine? When the array is built, when it's sorted, or when it is counted up at the end?
It's very important to learn to change one thing at a time, so that if something breaks, you know what is it. Then it becomes much simpler to work out why/how.
(By the way. If you do need to sort, then the QuickSort routine is likely to save you tons of time compared to the simple bubble-type sort we've been using.)
P.S. If the data is already sorted, so you remove the sort and the bug goes away, then that will tell you where it was wrong. :)
thank u sir for ur response will do that now.
yep... you might be right about speed of processing... by how much an hour? but if you're developing and you have a deadline. do what is the fastest right? :D
it's just a suggestion. sometimes we trade processing time, for faster output and flexibility. and what if you have a new requirement for you report? then what? :D redo the program again? that's what i'm saying... if the SCRIPT can do it... then it'll be faster in the long run... in terms of producing it.
cheers.
Galahad,
since that problem was for text file.. why put database business on textfle business.. i mean if the operation doesnt require database
and since we dont know why he is using that kind of format, we dont know ... so why dont we stick to the plan... and just help that guy..
and for the speed .. every one love speed.. thats why we create program to gain speed...
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Andrew Chanter |
last post by:
I am writing a little routine to perform the following operations from an
Acces 97 mdb:
1. create a fixed width text file
2. create/establish a table type link to the text file in Access
3....
|
by: siliconwafer |
last post by:
Hi All,
If I open a binary file in text mode and use text functions to
read it then will I be reading numbers as characters or actual
values?
What if I open a text file and read it using binary...
|
by: Lalasa |
last post by:
Hi,
Can anybody tell me how many cpu cycles File.copy would take and
how many cpu cycles File.Move would take?
CFile::Rename in C++ takes just one cpu cycle. As there is no
File.Rename in C#,...
|
by: Rob Leyshon |
last post by:
For whatever reason I am unable to use FileSystemObject
e.g. using the code:
Dim fso As New FileSystemObject
Everytime my program hits this it gives the error:
"Compile error: User-defined...
|
by: Andrew |
last post by:
Hi, im trying to create a small function which can create a binary tree
from the entries in a text file and after that we can perform all the
usual operations like search, insert and delete etc....
| |
by: ACC |
last post by:
Hi!
I'm developing an application to analyze some information that is
inside a text file. The size of the text file goes from 50Mb to
220Mb...
The text file is like a table, with "rows" and...
|
by: JJ |
last post by:
I have a need to input a large tab delimited text file, which I will parse
to check it has the expected columns, before allowing the user to submit it
to the database. The user may paste the file...
|
by: parthaspanda22 |
last post by:
How can I get to discard the contents of a text file from a specified
offset(
say, obtained from ftell)?
Sincerely.
|
by: tabassumpatil |
last post by:
Please send the c code for:
1.STACK OPERATIONS :
Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2.
2.QUEUE OPERATIONS :
Write a program to implement...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |