Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > For Next loop trouble


Reply
 
Thread Tools Display Modes
  #1  
Old 07-23-2012, 08:40 AM
HappyJoni's Avatar
HappyJoni HappyJoni is offline
Centurion
 
Join Date: Jun 2003
Location: New Jersey
Posts: 150
Default For Next loop trouble


I'm trying to read the first instance of an item in a list (Rpt_List(i)) and compare that against a field in record (CODE). If it's a match, I add to counter (Dat_CNT) and continue to read another record to see if there's another match (I'm reading the file sequentially, the records are already sorted in numerical order by the field I'm reading in). If there's not a match, I need to increment the Rpt_List index to get the next item in the list and do a comparison against the field (CODE) that I've already read. I need to keep incrementing the list index (i) and comparing without reading a new record. Once I have a match, I need to then read the next record. Also, I'm creating another list in this process.. I'm counting the number of matches for each item in the list. It works for the first item, but after that it seems to fall apart. I can't figure out what I'm doing wrong... anyone see my error??
Code:
For i = 0 To Rpt_List.Count - 1 If System.IO.File.Exists(Dat_FILE) = True Then Dim objreader As New System.IO.StreamReader(Dat_FILE) Do While objreader.Peek() <> -1 Dat_REC = objreader.ReadLine() & vbNewLine Code = Mid(Dat_REC, 96, 6) Rec_CNT = Rec_CNT + 1 Rpt_ListIn = Rpt_List(i) If Code = Rpt_ListIn Then Dat_CNT = Dat_CNT + 1 Else Dat_CNTout = Dat_CNT Dat_List.Add(Dat_CNTout) Dat_CNT = 0 Continue Do End If Loop ListBox2.DataSource = Dat_List Label1.Text = Dat_CNT & vbCrLf & Rec_CNT & " records read" Else MsgBox("File Does Not Exist") End If Next i

Last edited by Gruff; 07-23-2012 at 09:18 AM.
Reply With Quote
  #2  
Old 07-23-2012, 09:51 AM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,876
Default

Joni,

1) You appear to be using VB5/VB6 technology with VB.NET. VB.NET has some very nice tools that make this sort of thing much easier to do.

2) I would look at revamping your program flow concepts.
Basically you have two lists. One in a file and one in memory (Rpt_List).
You are looping through all of Rpt_List and opening and reading the entire file for every item in Rpt_List. File Access is much slower than working in memory.

If your file is really large then reading it line by line in a loop makes sense if it is not then I would read the whole thing into an array with perhaps the System.IO.File.ReadAllLines() Method.

Read the file once and find the matching values in Rpt_List inside that.

VB.NET has a method called 'Contains()' You can use this to return whether a value exists in Rpt_List without having to loop at all.

--- In Pseudo Code the concept would be ---
Loop through each line in the file
Get Item from line using String.Substring(95,6)
If Rpt_List.Contains(Item) then
...
Else
...
End If
End Loop
Display summation.

BTW Substring offsets are from 0 not 1.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #3  
Old 07-23-2012, 10:38 AM
HappyJoni's Avatar
HappyJoni HappyJoni is offline
Centurion
 
Join Date: Jun 2003
Location: New Jersey
Posts: 150
Talking

Hi Gruff... thanks for your response

Is it that obvious that I'm a VB6 lover?? lol
My company just switched me over to VB.net and refused to send me for training so I'm have a bit a rough time converting. I was never really 'super' at VB6 in the first place (I'm a 'junior' programmer).

The file I'm reading (DAT_FILE) is pretty large (at least I think so, it has over 33,000 records). Is this too large to read into memory?

Also, the file I want to count the occurances of a match in is the DAT_FILE. Looking over your suggestion it seems to me that you're counting the occurances in the list, but I could very well be wrong, I'm pretty lost at this point! lol

Thanks again
Joni
Reply With Quote
  #4  
Old 07-23-2012, 10:45 AM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

33000 records is probably not a problem.
Assuming 80 characters and unicode encoding, it would be a little more than 5 MB of memory.
I wouldn't worry about files fitting in memory unless they were 100s of MB in size.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 07-23-2012, 11:36 AM
HappyJoni's Avatar
HappyJoni HappyJoni is offline
Centurion
 
Join Date: Jun 2003
Location: New Jersey
Posts: 150
Default

Okay.. I took your advice and started over. I have the first part working, I am able to load the file into an array and then pull the field i need to work with and load it into a listbox so i can see that it is what I was expecting. It works great

Second part, not so great. Does Contains() count the number of occurances in an array or does it just simply tell you whether or not the the array 'contains' that value? Is there a way to count the number of times the array has a specific value with Contains()?



'FIRST PART - AWESOME!
Dim Code_list As New List(Of String)

If System.IO.File.Exists(Dat_FILE) = True Then
Dim Lines() As String = System.IO.File.ReadAllLines(Dat_FILE)
Dim CodeIndex = UBound(Lines)
For i = 0 To CodeIndex
Code = Mid(Lines(i), 96, 6)
Code_list.Add(Code)
Next
End If

ListBox4.DataSource = (Code_list)


'SECOND PART
For i = 0 To Rpt_List.Count - 1
Dat_CNT = 0
If Code_list.Contains(Rpt_List(i)) Then
Dat_CNT = Dat_CNT + 1
Dat_CNTout = Dat_CNT
Dat_List.Add(Dat_CNTout)
End If
Next

ListBox2.DataSource = (Dat_List)
Reply With Quote
  #6  
Old 07-23-2012, 04:00 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,876
Default

You are correct. Contains doesn't give you a count simply that at least one of that element exists.

To get a count you could go about it in several different ways. I guess I would lean heavily on the indexOf() method instead of the contains() method.

--- Untested Code ---
Code:
Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 'Gather your codes as before but do not add duplicates 'Store the count for all codes instead. 'This will reduce the number of items you have to loop through in total 'As well as having to make only one pass through the loop. Dim Code_list As New List(Of String) Dim Count_List As New List(Of Integer) 'FIRST PART If System.IO.File.Exists(Dat_FILE) = True Then Dim Lines() As String = System.IO.File.ReadAllLines(Dat_FILE) For i = 0 To Lines.Count - 1 Dim Code As String = Lines(i).Substring(95, 6) Dim nPos As Integer = Code_list.IndexOf(Code) If nPos < 0 Then Code_list.Add(Code) Count_List.Add(1) Else Count_List(nPos) += 1 End If Next End If 'Make one pass through your Rpt_list 'SECOND PART For i = 0 To Rpt_List.Count - 1 Dim Code2 As String = Rpt_List(i) Dim nPos As Integer = Code_list.IndexOf(Code2) If nPos >= 0 Then 'Assuming Dat_List is a listbox. Dat_List.Add(Code2 & " " & Count_List(nPos)) End If Next End Sub End Class
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 07-23-2012 at 04:09 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->