473,508 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

text file operations with vb6.0

25 New Member
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

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim fsys As New FileSystemObject
  3. Dim txtstream As TextStream
  4. Dim outstream As TextStream
  5.  
  6.  
  7. Private Sub cmdclick_Click()
  8.  
  9.  Dim j As Integer
  10.  Dim searchline As String   ' reads each line of txtstream
  11.  Dim filename As String     'file name which is dynamically generated
  12.  Dim homeid As String   'to store 12 numbers
  13.  Dim prevhomeid As String '  to comapre with other numbers
  14.  Dim channelname As String '  to store channelname
  15.  Dim prevchannelname As String  ' to compare prevchannelname
  16.  Dim channelcount As Integer  ' to give channelcount
  17.  
  18.  
  19.  
  20.   If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
  21.                MsgBox "file is open"
  22.              Else
  23.               MsgBox "file not found"
  24.              End If
  25.  
  26.                prevchannelname = ""
  27.                prevhomeid = ""
  28.                channelcount = 0
  29.  
  30.     Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
  31.        Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
  32.                Do Until txtstream.AtEndOfStream
  33.                    searchline = txtstream.ReadLine
  34.                       homeid = Mid(searchline, 1, 10)
  35.                    If prevhomeid <> homeid Then
  36.                             'do nothing`
  37.                           outstream.WriteLine (homeid)
  38.                         End If
  39.                              prevhomeid = homeid
  40.                Loop
  41.  
  42.                            MsgBox " Process completed"
  43. End Sub
  44.  
any help would be sincerely appreciated.
Mar 10 '07 #1
42 10257
Killer42
8,435 Recognized Expert Expert
...
NOW I AM NOT ABLE TO GET THIS i HAVE THE CODE pLEASE HAVE THE LOOK AT THE CODE
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim fsys As New FileSystemObject
  3. Dim txtstream As TextStream
  4. Dim outstream As TextStream
  5.  
  6. Private Sub cmdclick_Click()
  7.  
  8.  Dim j As Integer
  9.  Dim searchline As String      ' reads each line of txtstream
  10.  Dim filename As String        ' file name which is dynamically generated
  11.  Dim homeid As String          ' to store 12 numbers
  12.  Dim prevhomeid As String      ' to comapre with other numbers
  13.  Dim channelname As String     ' to store channelname
  14.  Dim prevchannelname As String ' to compare prevchannelname
  15.  Dim channelcount As Integer   ' to give channelcount
  16.  
  17.  
  18.  
  19.   If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
  20.     MsgBox "file is open"
  21.   Else
  22.     MsgBox "file not found"
  23.   End If
  24.  
  25.   prevchannelname = ""
  26.   prevhomeid = ""
  27.   channelcount = 0
  28.  
  29.   Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
  30.   Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
  31.   Do Until txtstream.AtEndOfStream
  32.     searchline = txtstream.ReadLine
  33.     homeid = Mid(searchline, 1, 10)
  34.     If prevhomeid <> homeid Then
  35.       'do nothing`
  36.       outstream.WriteLine (homeid)
  37.     End If
  38.     prevhomeid = homeid
  39.   Loop
  40.  
  41.   MsgBox " Process completed"
  42. 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.
Mar 10 '07 #2
vijaydiwakar
579 Contributor
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
  4.        Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
  5.                Do Until txtstream.AtEndOfStream
  6.                    searchline = txtstream.ReadLine
  7.                       homeid = Mid(searchline, 1, 10)
  8. 'here check this homeid with the distinct homeid in array say prevHomeId()
  9. 'if this home id doesnot exists then add this to array
  10. 'then again run this loop here u will check home id with your arraylist
  11. ' increment the counter and finally u'll get the ans
  12.                    If prevhomeid <> homeid Then
  13.                             'do nothing`
  14.                           outstream.WriteLine (homeid)
  15.                         End If
  16.                              prevhomeid = homeid
  17.                Loop
  18.  
  19.                            MsgBox " Process completed"
  20. End Sub
  21.  
Mar 10 '07 #3
Ronin
25 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
  4.        Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
  5.                Do Until txtstream.AtEndOfStream
  6.                    searchline = txtstream.ReadLine
  7.                       homeid = Mid(searchline, 1, 10)
  8. 'here check this homeid with the distinct homeid in array say prevHomeId()
  9. 'if this home id doesnot exists then add this to array
  10. 'then again run this loop here u will check home id with your arraylist
  11. ' increment the counter and finally u'll get the ans
  12.                    If prevhomeid <> homeid Then
  13.                             'do nothing`
  14.                           outstream.WriteLine (homeid)
  15.                         End If
  16.                              prevhomeid = homeid
  17.                Loop
  18.  
  19.                            MsgBox " Process completed"
  20. End Sub
  21.  
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.
Mar 12 '07 #4
Killer42
8,435 Recognized Expert Expert
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.
Mar 12 '07 #5
Ronin
25 New Member
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
Mar 13 '07 #6
Ronin
25 New Member
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]
Mar 15 '07 #7
Ronin
25 New Member
have a look at this if this is the right way.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Explicit
  3. Dim fsys As New FileSystemObject
  4. Dim txtstream As TextStream
  5. Dim outstream As TextStream
  6.  
  7.  
  8. Private Sub cmdclick_Click()
  9.  Dim j As Integer
  10.  Dim searchline As String
  11.  Dim filename As String
  12.  Dim homeid As String
  13.  Dim prevhomeid As String
  14.  Dim channelname As String
  15.  Dim prevchannelname As String
  16.  Dim channelcount As Integer
  17.  Dim channelarray(100000) As String
  18.  Dim i As Integer
  19.  
  20.     For j = 0 To lstmarket.ListCount - 1
  21.      If lstmarket.Selected(j) = True Then
  22.  
  23.  
  24.            If Len(Trim(txtyear.Text)) > 2 Or IsNumeric(Trim(txtyear.Text)) = False Then
  25.               MsgBox "this should be 2 digit number "
  26.               txtyear.Text = ""
  27.               txtyear.SetFocus
  28.            Exit Sub
  29.            End If
  30.  
  31.            If Trim(txtweek) <= 9 And Len(Trim(txtweek)) < 2 Or IsNumeric(txtweek.Text) = False Then
  32.               txtweek = "0" & Trim(txtweek)
  33.            End If
  34.  
  35.            If Trim(txtday) > 7 Or IsNumeric(txtday) = False Then
  36.                 MsgBox "Please enter number which is equal to or less than 7"
  37.                 txtday.Text = ""
  38.                 txtday.SetFocus
  39.            Exit Sub
  40.            End If
  41.  
  42.            filename = lstmarket.List(j) & Trim(txtyear) & Trim(txtweek) & Trim(txtday) & ".swd"
  43.               MsgBox ("you have selected the following" & filename)
  44.      End If
  45.    Next j
  46.  
  47.   '********main code for listing all the channels that the house id watch*******
  48.  
  49.              If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
  50.                MsgBox "file is open"
  51.              Else
  52.               MsgBox "file not found"
  53.              End If
  54.  
  55.                prevchannelname = ""
  56.                prevhomeid = ""
  57.  
  58.                channelcount = 0
  59.  
  60.     Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
  61.        Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
  62.                Do Until txtstream.AtEndOfStream
  63.                    searchline = txtstream.ReadLine
  64.                      homeid = Mid(searchline, 1, 10)
  65.                  For i = 0 To channelcount 
  66.                     If channelarray(i) <> homeid Then
  67.                       channelarray(i) = homeid
  68.                     End If
  69.                     If homeid <> channelarray(i) Then
  70.                       channelcount = channelcount + 1
  71.                    'channelcount = channelcount + 1
  72.                     End If
  73.  
  74.                  Next i
  75.                     'prevhomeid (homeid)
  76.                    'If prevhomeid <> homeid Then
  77.                     'If channelarray(i) <> homeid Then
  78.  
  79.  
  80.                             'do nothing`
  81.                        'channelname = Mid(homeid, 12, 5)
  82.                            'If prevchannelname <> channelname Then
  83.                               'channelcount = channelcount + 1
  84.                            'End If
  85.                        'outstream.WriteLine (homeid)
  86.                        outstream.WriteLine (homeid) & vbTab & channelcount
  87.                   ' End If
  88.                              'prevhomeid = homeid
  89.                Loop
  90.  
  91.                            MsgBox " Process completed"
  92. End Sub
  93.  
  94.  
still i am not able to get the desired output as mentioned above
Mar 15 '07 #8
Killer42
8,435 Recognized Expert Expert
I'm short on time, so I'm going to address one thing at a time. This piece of code...
Expand|Select|Wrap|Line Numbers
  1. For i = 0 To channelcount 
  2.   If channelarray(i) <> homeid Then
  3.     channelarray(i) = homeid
  4.   End If
  5.   If homeid <> channelarray(i) Then
  6.     channelcount = channelcount + 1
  7.   End If
  8. 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. :)
Mar 15 '07 #9
Ronin
25 New Member
I'm short on time, so I'm going to address one thing at a time. This piece of code...
Expand|Select|Wrap|Line Numbers
  1. For i = 0 To channelcount 
  2.   If channelarray(i) <> homeid Then
  3.     channelarray(i) = homeid
  4.   End If
  5.   If homeid <> channelarray(i) Then
  6.     channelcount = channelcount + 1
  7.   End If
  8. 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.:)
Mar 16 '07 #10
Killer42
8,435 Recognized Expert Expert
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.
Mar 19 '07 #11
Ronin
25 New Member
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
Mar 20 '07 #12
Killer42
8,435 Recognized Expert Expert
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.
Mar 21 '07 #13
Ronin
25 New Member
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
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Option Explicit
  4. Dim fsys As New FileSystemObject
  5. Dim txtstream As TextStream
  6. Dim outstream As TextStream
  7.  
  8.  
  9. Private Sub cmdclick_Click()
  10.  Dim j As Integer
  11.  Dim searchline As String
  12.  Dim filename As String
  13.  Dim homeid As String
  14.  Dim prevhomeid As String
  15.  Dim channelname As String
  16.  Dim channelcount As Integer
  17.  Dim channelarray(0 To 100000) As String
  18.  Dim newchannelarray(0 To 100000) As String
  19.  Dim carray As String
  20.  Dim prevcarray As String
  21.  Dim i As Double
  22.  
  23.  
  24.    prevcarray = ""
  25.     For j = 0 To lstmarket.ListCount - 1
  26.      If lstmarket.Selected(j) = True Then
  27.  
  28.  
  29.            If Len(Trim(txtyear.Text)) > 2 Or IsNumeric(Trim(txtyear.Text)) = False Then
  30.               MsgBox "this should be 2 digit number "
  31.               txtyear.Text = ""
  32.               txtyear.SetFocus
  33.            Exit Sub
  34.            End If
  35.  
  36.            If Trim(txtweek) <= 9 And Len(Trim(txtweek)) < 2 Or IsNumeric(txtweek.Text) = False Then
  37.               txtweek = "0" & Trim(txtweek)
  38.            End If
  39.  
  40.            If Trim(txtday) > 7 Or IsNumeric(txtday) = False Then
  41.                 MsgBox "Please enter number which is equal to or less than 7"
  42.                 txtday.Text = ""
  43.                 txtday.SetFocus
  44.            Exit Sub
  45.            End If
  46.  
  47.            filename = lstmarket.List(j) & Trim(txtyear) & Trim(txtweek) & Trim(txtday) & ".swd"
  48.               MsgBox ("you have selected the following" & filename)
  49.      End If
  50.    Next j
  51.  
  52.   '********main code for listing all the channels that the house id watch*******
  53.  
  54.              If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
  55.                MsgBox "file is open"
  56.              Else
  57.                MsgBox "file not found"
  58.              End If
  59.                'prevcarray = ""
  60.                'prevchannelname = ""
  61.                'prevhomeid = ""
  62.               channelcount = 0
  63.               'prevchannelarray(k) = ""
  64.     Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
  65.      Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
  66.                Do Until txtstream.AtEndOfStream
  67.                    searchline = txtstream.ReadLine
  68.                      homeid = Mid(searchline, 1, 10)
  69.                       channelname = Mid(searchline, 13, 5)
  70.                        channelarray(i) = homeid & channelname
  71.  
  72. 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.
  73. sir but i m not able to use the for loop to check in the array,i think
  74.                         If channelarray(i) <> channelarray(i + 1) Then
  75.                          outstream.WriteLine (channelarray(i))
  76.                         End If
  77.                          channelarray(i + 1) = channelarray(i)
  78.                                          Loop
  79.                    MsgBox "process completed"
  80.  
  81. End Sub
  82.  
  83.  
sir pls help.
Mar 26 '07 #14
Killer42
8,435 Recognized Expert Expert
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...
Expand|Select|Wrap|Line Numbers
  1.   Dim j As Integer
  2.   Dim searchline As String
  3.   Dim filename As String
  4.   Dim homeid As String
  5.   Dim prevhomeid As String
  6.   Dim channelname As String
  7.   Dim channelcount As Integer
  8.   Dim HomeAndChannel As String  ' <-- Added this variable.
  9.   Dim Exists As Boolean         ' <-- And this one.
  10.   Dim ChannelsToReport As Long  ' <-- This one, too.
  11.   Dim channelarray(0 To 100000) As String
  12.   ' Dim newchannelarray(0 To 100000) As String
  13.   ' Dim carray As String
  14.   ' Dim prevcarray As String
  15.   'Dim i As Double
  16.   Dim i As Long
  17.  
  18.   ' prevcarray = ""
  19.   For j = 0 To lstMarket.ListCount - 1
  20.     If lstMarket.Selected(j) = True Then
  21.       If Len(Trim(txtYear.Text)) > 2 Or IsNumeric(Trim(txtYear.Text)) = False Then
  22.         MsgBox "This should be 2 digit number "
  23.         txtYear.Text = ""
  24.         txtYear.SetFocus
  25.         Exit Sub
  26.       End If
  27.  
  28.       If Trim(txtWeek) <= 9 And Len(Trim(txtWeek)) < 2 Or (IsNumeric(txtWeek.Text) = False) Then
  29.         txtWeek = "0" & Trim(txtWeek)
  30.       End If
  31.  
  32.       If Trim(txtDay) > 7 Or (IsNumeric(txtDay) = False) Then
  33.         MsgBox "Please enter number which is equal to or less than 7"
  34.         txtDay.Text = ""
  35.         txtDay.SetFocus
  36.         Exit Sub
  37.       End If
  38.  
  39.       filename = lstMarket.List(j) & Trim(txtYear) & Trim(txtWeek) & Trim(txtDay) & ".swd"
  40.       MsgBox ("you have selected the following : " & filename)
  41.     End If
  42.   Next j
  43.  
  44.   '********main code for listing all the channels that the house id watch*******
  45.  
  46.   If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
  47.     MsgBox "file is open"
  48.   Else
  49.     MsgBox "file not found"
  50.     Exit Sub
  51.   End If
  52.   'prevcarray = ""
  53.   'prevchannelname = ""
  54.   'prevhomeid = ""
  55.   channelcount = 0
  56.   'prevchannelarray(k) = ""
  57.   Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
  58.   Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
  59.  
  60.  
  61.  
  62.   ' ****************************************
  63.   ' Step 1: Build an array of all unique homeid/channel combinations
  64.   ' ****************************************
  65.  
  66.   Do Until txtstream.AtEndOfStream
  67.     searchline = txtstream.ReadLine
  68.     homeid = Mid(searchline, 1, 10)
  69.     channelname = Mid(searchline, 13, 5)
  70.     'channelarray(i) = homeid & channelname
  71.     HomeAndChannel = homeid & channelname
  72.  
  73.  
  74.     ' Check whether we have already seen this combination...
  75.     Exists = False
  76.     For i = 1 To channelcount
  77.       If channelarray(i) = HomeAndChannel Then
  78.         Exists = True
  79.         Exit For
  80.       End If
  81.     Next
  82.  
  83.     ' If not, add it to the array.
  84.     If Not Exists Then
  85.       channelcount = channelcount + 1
  86.       channelarray(channelcount) = HomeAndChannel
  87.     End If
  88.   Loop
  89.  
  90.  
  91.  
  92.   ' ****************************************
  93.   ' Step 2: Sort the array. I'll leave this to you, as I'm short on time
  94.   ' ****************************************
  95.  
  96.  
  97.  
  98.  
  99.  
  100.   ' ****************************************
  101.   ' Step 3: Scan the array and report the number of channels per homeid
  102.   ' ****************************************
  103.  
  104.   ChannelsToReport = 1
  105.   prevhomeid = Left$(channelarray(1), 10)
  106.   For i = 2 To channelcount
  107.     homeid = Left$(channelarray(i), 10)
  108.     If homeid = prevhomeid Then
  109.       ChannelsToReport = ChannelsToReport + 1
  110.     Else
  111.       outstream.WriteLine prevhomeid & " " & Format(ChannelsToReport)
  112.       ChannelsToReport = 1
  113.       prevhomeid = homeid
  114.     End If
  115.   Next
  116.   MsgBox "process completed"
Mar 26 '07 #15
Ronin
25 New Member
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...
Expand|Select|Wrap|Line Numbers
  1.   Dim j As Integer
  2.   Dim searchline As String
  3.   Dim filename As String
  4.   Dim homeid As String
  5.   Dim prevhomeid As String
  6.   Dim channelname As String
  7.   Dim channelcount As Integer
  8.   Dim HomeAndChannel As String  ' <-- Added this variable.
  9.   Dim Exists As Boolean         ' <-- And this one.
  10.   Dim ChannelsToReport As Long  ' <-- This one, too.
  11.   Dim channelarray(0 To 100000) As String
  12.   ' Dim newchannelarray(0 To 100000) As String
  13.   ' Dim carray As String
  14.   ' Dim prevcarray As String
  15.   'Dim i As Double
  16.   Dim i As Long
  17.  
  18.   ' prevcarray = ""
  19.   For j = 0 To lstMarket.ListCount - 1
  20.     If lstMarket.Selected(j) = True Then
  21.       If Len(Trim(txtYear.Text)) > 2 Or IsNumeric(Trim(txtYear.Text)) = False Then
  22.         MsgBox "This should be 2 digit number "
  23.         txtYear.Text = ""
  24.         txtYear.SetFocus
  25.         Exit Sub
  26.       End If
  27.  
  28.       If Trim(txtWeek) <= 9 And Len(Trim(txtWeek)) < 2 Or (IsNumeric(txtWeek.Text) = False) Then
  29.         txtWeek = "0" & Trim(txtWeek)
  30.       End If
  31.  
  32.       If Trim(txtDay) > 7 Or (IsNumeric(txtDay) = False) Then
  33.         MsgBox "Please enter number which is equal to or less than 7"
  34.         txtDay.Text = ""
  35.         txtDay.SetFocus
  36.         Exit Sub
  37.       End If
  38.  
  39.       filename = lstMarket.List(j) & Trim(txtYear) & Trim(txtWeek) & Trim(txtDay) & ".swd"
  40.       MsgBox ("you have selected the following : " & filename)
  41.     End If
  42.   Next j
  43.  
  44.   '********main code for listing all the channels that the house id watch*******
  45.  
  46.   If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
  47.     MsgBox "file is open"
  48.   Else
  49.     MsgBox "file not found"
  50.     Exit Sub
  51.   End If
  52.   'prevcarray = ""
  53.   'prevchannelname = ""
  54.   'prevhomeid = ""
  55.   channelcount = 0
  56.   'prevchannelarray(k) = ""
  57.   Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
  58.   Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
  59.  
  60.  
  61.  
  62.   ' ****************************************
  63.   ' Step 1: Build an array of all unique homeid/channel combinations
  64.   ' ****************************************
  65.  
  66.   Do Until txtstream.AtEndOfStream
  67.     searchline = txtstream.ReadLine
  68.     homeid = Mid(searchline, 1, 10)
  69.     channelname = Mid(searchline, 13, 5)
  70.     'channelarray(i) = homeid & channelname
  71.     HomeAndChannel = homeid & channelname
  72.  
  73.  
  74.     ' Check whether we have already seen this combination...
  75.     Exists = False
  76.     For i = 1 To channelcount
  77.       If channelarray(i) = HomeAndChannel Then
  78.         Exists = True
  79.         Exit For
  80.       End If
  81.     Next
  82.  
  83.     ' If not, add it to the array.
  84.     If Not Exists Then
  85.       channelcount = channelcount + 1
  86.       channelarray(channelcount) = HomeAndChannel
  87.     End If
  88.   Loop
  89.  
  90.  
  91.  
  92.   ' ****************************************
  93.   ' Step 2: Sort the array. I'll leave this to you, as I'm short on time
  94.   ' ****************************************
  95.  
  96.  
  97.  
  98.  
  99.  
  100.   ' ****************************************
  101.   ' Step 3: Scan the array and report the number of channels per homeid
  102.   ' ****************************************
  103.  
  104.   ChannelsToReport = 1
  105.   prevhomeid = Left$(channelarray(1), 10)
  106.   For i = 2 To channelcount
  107.     homeid = Left$(channelarray(i), 10)
  108.     If homeid = prevhomeid Then
  109.       ChannelsToReport = ChannelsToReport + 1
  110.     Else
  111.       outstream.WriteLine prevhomeid & " " & Format(ChannelsToReport)
  112.       ChannelsToReport = 1
  113.       prevhomeid = homeid
  114.     End If
  115.   Next
  116.   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

Expand|Select|Wrap|Line Numbers
  1. 'sorting of array
  2.                            For i = LBound(channelarray) To UBound(channelarray) - 1
  3.                              For k = i + 1 To Ubound(channelarray) - 1 <-------- this step gives error overflow.
  4.                               If channelarray(i) > channelarray(k) Then
  5.                                 tmpsort = channelarray(i)
  6.                                 channelarray(i) = channelarray(k)
  7.                                 channelarray(k) = tmpsort
  8.                               End If
  9.                              Next
  10.                             Next
  11.  

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 .
Mar 27 '07 #16
Killer42
8,435 Recognized Expert Expert
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.)
Mar 27 '07 #17
Ronin
25 New Member
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.
Mar 28 '07 #18
Killer42
8,435 Recognized Expert Expert
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?
Mar 28 '07 #19
Killer42
8,435 Recognized Expert Expert
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...
Expand|Select|Wrap|Line Numbers
  1. Change this line...
  2.   For k = i + 1 To Ubound(channelarray) - 1
  3. to...
  4.   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.
Mar 28 '07 #20
Ronin
25 New Member
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...
Expand|Select|Wrap|Line Numbers
  1. Change this line...
  2.   For k = i + 1 To Ubound(channelarray) - 1
  3. to...
  4.   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.
Mar 28 '07 #21
Ronin
25 New Member
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
Mar 28 '07 #22
Killer42
8,435 Recognized Expert Expert
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.
Mar 28 '07 #23
Ronin
25 New Member
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.
Mar 28 '07 #24
Ronin
25 New Member
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Explicit
  3. Dim fsys As New FileSystemObject
  4. Dim txtstream As TextStream
  5. Dim outstream As TextStream
  6.  
  7.  
  8.  
  9. Public Sub cmdclick_Click()
  10.  Dim j As Integer
  11.  Dim searchline As String
  12.  Dim filename As String
  13.  Dim homeid As String
  14.  Dim prevhomeid As String
  15.  Dim channelname As String
  16.  Dim channelcount As Integer
  17.  Dim channelarray(0 To 100000) As String
  18.  Dim HomeAndChannel As String
  19.  Dim Exists As Boolean
  20.  Dim ChannelsToReport As Long
  21.  Dim carray As String
  22.  Dim prevcarray As String
  23.  Dim i As Double
  24.  Dim k As Double
  25.  Dim tmpsort As String
  26.  
  27.  
  28.  
  29.  
  30.  
  31.    prevcarray = ""
  32.     For j = 0 To lstmarket.ListCount - 1
  33.      If lstmarket.Selected(j) = True Then
  34.  
  35.  
  36.            If Len(Trim(txtyear.Text)) > 2 Or IsNumeric(Trim(txtyear.Text)) = False Then
  37.               MsgBox "this should be 2 digit number "
  38.               txtyear.Text = ""
  39.               txtyear.SetFocus
  40.            Exit Sub
  41.            End If
  42.  
  43.            If Trim(txtweek) <= 9 And Len(Trim(txtweek)) < 2 Or IsNumeric(txtweek.Text) = False Then
  44.               txtweek = "0" & Trim(txtweek)
  45.            End If
  46.  
  47.            If Trim(txtday) > 7 Or IsNumeric(txtday) = False Then
  48.                 MsgBox "Please enter number which is equal to or less than 7"
  49.                 txtday.Text = ""
  50.                 txtday.SetFocus
  51.            Exit Sub
  52.            End If
  53.  
  54.            filename = lstmarket.List(j) & Trim(txtyear) & Trim(txtweek) & Trim(txtday) & ".swd"
  55.               MsgBox ("you have selected the following" & filename)
  56.      End If
  57.    Next j
  58.  
  59.   '********main code for listing all the channels that the house id watch*******
  60.  
  61.              If fsys.FileExists("C:\rohit program\exercise\SWD\" & filename) = True Then
  62.                MsgBox "file is open"
  63.              Else
  64.                MsgBox "file not found"
  65.              End If
  66.                              channelcount = 0
  67.                  Set outstream = fsys.OpenTextFile("c:\channelcount.txt", ForWriting, True)
  68.      Set txtstream = fsys.OpenTextFile("C:\rohit program\exercise\SWD\" & filename, ForReading, False)
  69.                Do Until txtstream.AtEndOfStream
  70.                    searchline = txtstream.ReadLine
  71.                      homeid = Mid(searchline, 1, 10)
  72.                       channelname = Mid(searchline, 13, 5)
  73.                         channelarray(i) = homeid & channelname
  74.                          HomeAndChannel = homeid & channelname
  75.                                Exists = False
  76.                             For i = 0 To channelcount
  77.                                 If channelarray(i) = HomeAndChannel Then
  78.                                 Exists = True
  79.                             Exit For
  80.                                 End If
  81.  
  82.                             Next
  83.  
  84.                                If Not Exists Then
  85.                                  channelcount = channelcount + 1
  86.                                  channelarray(channelcount) = HomeAndChannel
  87.  
  88.                Loop
  89.  
  90.             'sorting array<----------- code for sorting
  91.  
  92.                             For i = LBound(channelarray) To UBound(channelarray) - 1
  93.                              For k = UBound(channelarray) - 1 To i Step -1
  94.                                If channelarray(i) > channelarray(k) Then
  95.                                   tmpsort = channelarray(k)
  96.                                   channelarray(k) = channelarray(i)
  97.                                   channelarray(i) = tmpsort
  98.                                End If
  99.                             Next
  100.                         Next
  101.  
  102.  
  103.  
  104.                                  ChannelsToReport = 1
  105.                                  prevhomeid = Left$(channelarray(i), 10)
  106.                                             For i = 1 To channelcount
  107.                                               homeid = Left$(channelarray(i), 10)
  108.                                                 If homeid = prevhomeid Then
  109.                                                      ChannelsToReport = ChannelsToReport + 1
  110.                                                 Else
  111.                                                        outstream.WriteLine prevhomeid & " " & Format(ChannelsToReport)
  112.                                                        ChannelsToReport = 1
  113.                                                        prevhomeid = homeid
  114.  
  115.                                                 End If
  116.                                             Next
  117.                                End If
  118.  
  119.          'Loop<---am i right in putting this loop here
  120.                    MsgBox "process completed"
  121.  
  122. End Sub
  123.  
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.
Mar 28 '07 #25
Ronin
25 New Member
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.
Mar 28 '07 #26
Killer42
8,435 Recognized Expert Expert
Expand|Select|Wrap|Line Numbers
  1. ...
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.
Mar 28 '07 #27
pongscript
27 New Member
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]
Mar 28 '07 #28
pongscript
27 New Member
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# .
Mar 28 '07 #29
galahad
2 New Member
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.
Mar 28 '07 #30
pongscript
27 New Member
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..
Mar 28 '07 #31
galahad
2 New Member
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.
Mar 29 '07 #32
Killer42
8,435 Recognized Expert Expert
...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...
  1. 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
  2. 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.
Mar 29 '07 #33
Killer42
8,435 Recognized Expert Expert
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...
Mar 29 '07 #34
Killer42
8,435 Recognized Expert Expert
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.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdclick_Click()
  2.  
  3.   Dim i As Long             ' General loop counter.
  4.   Dim j As Integer          ' General loop counter.
  5.   Dim FileName As String    ' File to be scanned.
  6.   Dim SearchLine As String  ' Input line from data file.
  7.  
  8.   ' Used in building and sorting array of unique homeid/channel combinations...
  9.   Dim Exists As Boolean
  10.   Dim HomeAndChannel As String
  11.   Dim ChannelArray(0 To 100000) As String
  12.   Dim ArrayCount As Long                   ' Number of entries placed in the array.
  13.  
  14.   ' Used in counting the unique channels per homeid...
  15.   Dim HomeId As String
  16.   Dim PrevHomeId As String
  17.   Dim ChannelCount As Integer
  18.  
  19.  
  20.   ' prevcarray = ""
  21. '  For j = 0 To lstMarket.ListCount - 1
  22. '    If lstMarket.Selected(j) = True Then
  23. '      If Len(Trim(txtYear.Text)) > 2 Or IsNumeric(Trim(txtYear.Text)) = False Then
  24. '        MsgBox "This should be 2 digit number "
  25. '        txtYear.Text = ""
  26. '        txtYear.SetFocus
  27. '        Exit Sub
  28. '      End If
  29. '
  30. '      If Trim(txtWeek) <= 9 And Len(Trim(txtWeek)) < 2 Or (IsNumeric(txtWeek.Text) = False) Then
  31. '        txtWeek = "0" & Trim(txtWeek)
  32. '      End If
  33. '
  34. '      If Trim(txtDay) > 7 Or (IsNumeric(txtDay) = False) Then
  35. '        MsgBox "Please enter number which is equal to or less than 7"
  36. '        txtDay.Text = ""
  37. '        txtDay.SetFocus
  38. '        Exit Sub
  39. '      End If
  40. '
  41. '      filename = lstMarket.List(j) & Trim(txtYear) & Trim(txtWeek) & Trim(txtDay) & ".swd"
  42. '      MsgBox ("you have selected the following : " & filename)
  43. '    End If
  44. '  Next j
  45.  
  46.   FileName = App.Path & "\Sample.swd"
  47.  
  48.   ' ********main code for listing all the channels that the house id watch*******
  49.  
  50.   If fsys.FileExists(FileName) Then
  51.     MsgBox "file is open"
  52.   Else
  53.     MsgBox "file not found"
  54.     Exit Sub
  55.   End If
  56.  
  57.   Set txtstream = fsys.OpenTextFile(FileName, ForReading, False)
  58.   Set outstream = fsys.OpenTextFile(App.Path & "\ChannelCount.txt", ForWriting, True)
  59.  
  60.  
  61.  
  62.   ' ****************************************
  63.   ' Step 1: Build an array of all unique homeid/channel combinations
  64.   ' ****************************************
  65.  
  66.   ArrayCount = 0
  67.   Do Until txtstream.AtEndOfStream
  68.     SearchLine = txtstream.ReadLine
  69.     HomeAndChannel = Left(SearchLine, 10) & Mid(SearchLine, 13, 5)
  70.     ' Check whether we have already seen this combination.
  71.     Exists = False
  72.     For i = 1 To ArrayCount
  73.       If ChannelArray(i) = HomeAndChannel Then
  74.         Exists = True
  75.         Exit For
  76.       End If
  77.     Next
  78.     ' If not, add it to the array.
  79.     If Not Exists Then
  80.       ArrayCount = ArrayCount + 1
  81.       ChannelArray(ArrayCount) = HomeAndChannel
  82.     End If
  83.   Loop
  84.  
  85.  
  86.  
  87.   ' ****************************************
  88.   ' Step 2: Sort the array.
  89.   ' ****************************************
  90.  
  91.   Dim k As Long
  92.   Dim tmpsort As String
  93.  
  94.   For i = 1 To ArrayCount - 1
  95.     For j = ArrayCount - 1 To i Step -1
  96.       k = j + 1
  97.       If ChannelArray(j) > ChannelArray(k) Then
  98.         tmpsort = ChannelArray(j)
  99.         ChannelArray(j) = ChannelArray(k)
  100.         ChannelArray(k) = tmpsort
  101.       End If
  102.     Next
  103.   Next
  104.  
  105.  
  106.  
  107.   ' ****************************************
  108.   ' Step 3: Scan the array and report the number of channels per homeid
  109.   ' ****************************************
  110.  
  111.   ChannelCount = 1
  112.   PrevHomeId = Left$(ChannelArray(1), 10)
  113.   For i = 2 To ArrayCount
  114.     HomeId = Left$(ChannelArray(i), 10)
  115.     If HomeId = PrevHomeId Then
  116.       ChannelCount = ChannelCount + 1
  117.     Else
  118.       outstream.WriteLine PrevHomeId & " " & Format(ChannelCount)
  119.       ChannelCount = 1
  120.       PrevHomeId = HomeId
  121.     End If
  122.   Next
  123.   ' Write out the last entry, which will otherwise be missed.
  124.   outstream.WriteLine PrevHomeId & " " & Format(ChannelCount)
  125.   outstream.Close
  126.   txtstream.Close
  127.  
  128.   MsgBox "process completed"
  129.  
  130. End Sub
Mar 29 '07 #35
Ronin
25 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdclick_Click()
  2.  
  3.   Dim i As Long             ' General loop counter.
  4.   Dim j As Integer          ' General loop counter.
  5.   Dim FileName As String    ' File to be scanned.
  6.   Dim SearchLine As String  ' Input line from data file.
  7.  
  8.   ' Used in building and sorting array of unique homeid/channel combinations...
  9.   Dim Exists As Boolean
  10.   Dim HomeAndChannel As String
  11.   Dim ChannelArray(0 To 100000) As String
  12.   Dim ArrayCount As Long                   ' Number of entries placed in the array.
  13.  
  14.   ' Used in counting the unique channels per homeid...
  15.   Dim HomeId As String
  16.   Dim PrevHomeId As String
  17.   Dim ChannelCount As Integer
  18.  
  19.  
  20.   ' prevcarray = ""
  21. '  For j = 0 To lstMarket.ListCount - 1
  22. '    If lstMarket.Selected(j) = True Then
  23. '      If Len(Trim(txtYear.Text)) > 2 Or IsNumeric(Trim(txtYear.Text)) = False Then
  24. '        MsgBox "This should be 2 digit number "
  25. '        txtYear.Text = ""
  26. '        txtYear.SetFocus
  27. '        Exit Sub
  28. '      End If
  29. '
  30. '      If Trim(txtWeek) <= 9 And Len(Trim(txtWeek)) < 2 Or (IsNumeric(txtWeek.Text) = False) Then
  31. '        txtWeek = "0" & Trim(txtWeek)
  32. '      End If
  33. '
  34. '      If Trim(txtDay) > 7 Or (IsNumeric(txtDay) = False) Then
  35. '        MsgBox "Please enter number which is equal to or less than 7"
  36. '        txtDay.Text = ""
  37. '        txtDay.SetFocus
  38. '        Exit Sub
  39. '      End If
  40. '
  41. '      filename = lstMarket.List(j) & Trim(txtYear) & Trim(txtWeek) & Trim(txtDay) & ".swd"
  42. '      MsgBox ("you have selected the following : " & filename)
  43. '    End If
  44. '  Next j
  45.  
  46.   FileName = App.Path & "\Sample.swd"
  47.  
  48.   ' ********main code for listing all the channels that the house id watch*******
  49.  
  50.   If fsys.FileExists(FileName) Then
  51.     MsgBox "file is open"
  52.   Else
  53.     MsgBox "file not found"
  54.     Exit Sub
  55.   End If
  56.  
  57.   Set txtstream = fsys.OpenTextFile(FileName, ForReading, False)
  58.   Set outstream = fsys.OpenTextFile(App.Path & "\ChannelCount.txt", ForWriting, True)
  59.  
  60.  
  61.  
  62.   ' ****************************************
  63.   ' Step 1: Build an array of all unique homeid/channel combinations
  64.   ' ****************************************
  65.  
  66.   ArrayCount = 0
  67.   Do Until txtstream.AtEndOfStream
  68.     SearchLine = txtstream.ReadLine
  69.     HomeAndChannel = Left(SearchLine, 10) & Mid(SearchLine, 13, 5)
  70.     ' Check whether we have already seen this combination.
  71.     Exists = False
  72.     For i = 1 To ArrayCount
  73.       If ChannelArray(i) = HomeAndChannel Then
  74.         Exists = True
  75.         Exit For
  76.       End If
  77.     Next
  78.     ' If not, add it to the array.
  79.     If Not Exists Then
  80.       ArrayCount = ArrayCount + 1
  81.       ChannelArray(ArrayCount) = HomeAndChannel
  82.     End If
  83.   Loop
  84.  
  85.  
  86.  
  87.   ' ****************************************
  88.   ' Step 2: Sort the array.
  89.   ' ****************************************
  90.  
  91.   Dim k As Long
  92.   Dim tmpsort As String
  93.  
  94.   For i = 1 To ArrayCount - 1
  95.     For j = ArrayCount - 1 To i Step -1
  96.       k = j + 1
  97.       If ChannelArray(j) > ChannelArray(k) Then
  98.         tmpsort = ChannelArray(j)
  99.         ChannelArray(j) = ChannelArray(k)
  100.         ChannelArray(k) = tmpsort
  101.       End If
  102.     Next
  103.   Next
  104.  
  105.  
  106.  
  107.   ' ****************************************
  108.   ' Step 3: Scan the array and report the number of channels per homeid
  109.   ' ****************************************
  110.  
  111.   ChannelCount = 1
  112.   PrevHomeId = Left$(ChannelArray(1), 10)
  113.   For i = 2 To ArrayCount
  114.     HomeId = Left$(ChannelArray(i), 10)
  115.     If HomeId = PrevHomeId Then
  116.       ChannelCount = ChannelCount + 1
  117.     Else
  118.       outstream.WriteLine PrevHomeId & " " & Format(ChannelCount)
  119.       ChannelCount = 1
  120.       PrevHomeId = HomeId
  121.     End If
  122.   Next
  123.   ' Write out the last entry, which will otherwise be missed.
  124.   outstream.WriteLine PrevHomeId & " " & Format(ChannelCount)
  125.   outstream.Close
  126.   txtstream.Close
  127.  
  128.   MsgBox "process completed"
  129.  
  130. 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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Command1_Click()
  3.     Dim MemberId As String
  4.     Dim Channel As String
  5.     Dim MemberChannel As String
  6.     Dim MemberChannelArray() As String
  7.     Dim Cnt As Integer
  8.     Dim X As String
  9.     Me.MousePointer = 11
  10.     Cnt = 0
  11.     Open App.Path & "\10117055.swd" For Input As #1
  12.     'This loop will store only one row for a member channel combination in an array
  13.     Do While Not EOF(1)
  14.         Line Input #1, X
  15.         MemberId = left(X, 10)
  16.         Channel = Mid(X, 13, 5)
  17.         MemberChannel = MemberId + Channel
  18.  
  19.         If Cnt = 0 Then
  20.             Cnt = Cnt + 1
  21.             ReDim Preserve MemberChannelArray(Cnt)
  22.             MemberChannelArray(Cnt) = MemberChannel
  23.         Else
  24.             If bsearcH(MemberChannelArray, MemberChannel, 1, Cnt) = -1 Then
  25.                 Cnt = Cnt + 1
  26.                 ReDim Preserve MemberChannelArray(Cnt)
  27.                 MemberChannelArray(Cnt) = MemberChannel
  28.             End If
  29.         End If
  30.     Loop
  31.     Close #1
  32.     'Sorting the array
  33.     Call qsorT(MemberChannelArray, 1, Cnt)
  34.     Dim LastId As String
  35.     Dim i As Integer
  36.     Dim MemCnt As Integer
  37.     MemCnt = 1
  38.     LastId = left(MemberChannelArray(1), 10)
  39.     Open App.Path & "\output.txt" For Output As #2
  40.     For i = 2 To Cnt
  41.         If LastId <> left(MemberChannelArray(i), 10) Then
  42.             Print #2, LastId; " "; MemCnt
  43.             LastId = left(MemberChannelArray(i), 10)
  44.             MemCnt = 1
  45.         Else
  46.             MemCnt = MemCnt + 1
  47.         End If
  48.     Next
  49.     Print #2, LastId; " "; MemCnt
  50.     Close #2
  51.     Me.MousePointer = 0
  52. End Sub
  53. 'Function for searching an item in an array using Binary Search Algorithm
  54. Private Function bsearcH(lsT() As String, sst As String, ByVal lB As Integer, ByVal uB As Integer) As Integer
  55.     Dim mD As Integer
  56.     Do While uB >= lB
  57.         mD = (uB + lB) / 2
  58.         If lsT(mD) = sst Then
  59.             bsearcH = mD
  60.             Exit Function
  61.         End If
  62.         If sst < lsT(mD) Then
  63.             uB = mD - 1
  64.         End If
  65.         If sst > lsT(mD) Then
  66.             lB = mD + 1
  67.         End If
  68.     Loop
  69.     bsearcH = -1
  70. End Function
  71. 'Subroutine to perform a sort based on Quick Sort Algorithm
  72. Public Sub qsorT(in_array() As String, left As Integer, right As Integer)
  73.     Dim current As Integer, last As Integer
  74.     If left >= right Then
  75.         Exit Sub
  76.     End If
  77.     Call swaP(in_array, left, (left + right) / 2)
  78.  
  79.     last = left
  80.     For current = left + 1 To right
  81.         If in_array(current) < in_array(left) Then
  82.               Call swaP(in_array, last, current)
  83.         End If
  84.     Next
  85.     Call swaP(in_array, left, last)
  86.     Call qsorT(in_array, left, last - 1)
  87.     Call qsorT(in_array, last + 1, right)
  88.  
  89. End Sub
  90.  
  91. Private Sub swaP(in_array() As String, i As Integer, j As Integer)
  92.     Dim temp As String
  93.     temp = in_array(i)
  94.     in_array(i) = in_array(j)
  95.     in_array(j) = temp
  96. End Sub
  97.  
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
Mar 29 '07 #36
Killer42
8,435 Recognized Expert Expert
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.
Mar 29 '07 #37
Ronin
25 New Member
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
Mar 29 '07 #38
Killer42
8,435 Recognized Expert Expert
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.
Mar 29 '07 #39
Ronin
25 New Member
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.
Mar 29 '07 #40
Killer42
8,435 Recognized Expert Expert
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. :)
Mar 29 '07 #41
Ronin
25 New Member
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.
Mar 29 '07 #42
pongscript
27 New Member
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...
Apr 6 '07 #43

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

Similar topics

1
12198
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....
8
1773
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...
7
2566
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#,...
4
5279
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...
1
4383
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....
13
6058
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...
13
2782
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...
5
1973
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.
0
2158
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...
0
7124
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
7498
tracyyun
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...
0
5629
agi2029
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,...
1
5053
isladogs
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...
0
4707
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...
0
3195
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...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
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...

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.