Looping through 2D array to find match to 2 variables

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb Hi,<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<span style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb Im working on a program in VB 2005 that counts the number of instances, per record, that a certain integer
combination is encountered, without counting duplicates. Not sure if you need a full explanation of what Im working with and trying to accomplish but I supposed it couldnt hurt <img alt="" border="0" src="http://www.computerhelp.forum/x_images/images/smilies/smile.gif" title="Smilie" style="vertical-align:middle; font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb <br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<span style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb Each record is a different length and the http://www.computerhelp.forum/showthread.php?t=325392#" id="x_x_itxthook0 " style="border:0px none transparent; color:#3a3a3a; text-decoration:initial; font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:transparent; padding:0px; display:inline <span class="x_x_itxtrst x_x_itxtrstspan x_x_itxtnowrap" id="x_x_itxthook0p" style="height:auto; right:auto; bottom:auto; border:0px; background-color:transparent; font-family:inherit; font-size:inherit; float:none!important; display:inline!important; white-space:nowrap!important; padding:0px!important; margin:0px!important <span class="x_x_itxtrst x_x_itxtrstspan x_x_itxtnowrap x_x_itxtnewhookspan" id="x_x_itxthook0w" style="float:none; height:auto; right:auto; bottom:auto; border-width:0px 0px 1px; border-style:none none solid; border-color:#00cc00; background-color:transparent; display:inline; white-space:normal; font-family:inherit; color:#009900; padding:0px 0px 1px!important; margin:0px!important; text-decoration:underline!important data<img height="NaN" id="x_x_itxthook0icon" src="http://images.intellitxt.com/ast/adTypes/icon1.png" width="NaN" style="right:auto; bottom:auto; background-color:transparent; white-space:normal; max-height:none; float:none!important; padding:0px 0px 0px 4px!important; margin:0px!important; display:inline!important; border:0px!important; max-width:none!important; vertical-align:baseline!important <span style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb I
need begins in in column 135 and continues till the end of the record (where ever that may be, like I said, all records are different lengths). Im splitting the data into 7 column blocks, then, each 7 column block is split into 2 variables, the first a length
of 4 (this is the station code) and the second a length of 3 (this is the day code). Depending on the value of the station code, there are 3 possible station types (broadcast, cable and other). I have a variable Im assigning a value to based on what the
station type is - if broadcast -station type = 1, if cable - station type = 2 and if other - station type = 3. Im creating an array that contains day code and station type. This array exists only while im in a record and should reset when a new record is
read and refill. Before I add to the array, I want to first check if what Im adding already exists within the array. So, the first time I add, its obviously fine because the array is empty. The second time, there would only be one combination of day code
and station type, so I would only have 1 to check against. The third time there would be 2 to check against.. and so on. <br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<span style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb Im running into an issue, I believe, in the loop Im using to iterate through the array to check for duplicates.
Since the array grows every time I check, I think Im doing it wrong because my output is not what Im expecting. I should add, whether its a duplicate or not, it still gets added to the array, it only changes which counter Im adding to.<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<span style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb Hope I explained that okay... If someone could please take a look at my code, particularly my For- http://www.computerhelp.forum/showthread.php?t=325392#" id="x_x__GPLITA_2" title="Click to Continue > by Savings Explorer Next <span style=" style="color:#3a3a3a; font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb loop,
and let me know what Im doing wrong I would really appreciate it!<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<span style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb Im terrible dealing with arrays! I avoid them when I can though I know my life would be a lot http://www.computerhelp.forum/showthread.php?t=325392#" id="x_x_itxthook1 " style="border:0px none transparent; color:#3a3a3a; text-decoration:initial; font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:transparent; padding:0px; display:inline <span class="x_x_itxtrst x_x_itxtrstspan x_x_itxtnowrap" id="x_x_itxthook1p" style="height:auto; right:auto; bottom:auto; border:0px; background-color:transparent; font-family:inherit; font-size:inherit; float:none!important; display:inline!important; white-space:nowrap!important; padding:0px!important; margin:0px!important <span class="x_x_itxtrst x_x_itxtrstspan x_x_itxtnowrap x_x_itxtnewhookspan" id="x_x_itxthook1w" style="float:none; height:auto; right:auto; bottom:auto; border-width:0px 0px 1px; border-style:none none solid; border-color:#00cc00; background-color:transparent; display:inline; white-space:normal; font-family:inherit; color:#009900; padding:0px 0px 1px!important; margin:0px!important; text-decoration:underline!important easier<img height="NaN" id="x_x_itxthook1icon" src="http://images.intellitxt.com/ast/adTypes/icon1.png" width="NaN" style="right:auto; bottom:auto; background-color:transparent; white-space:normal; max-height:none; float:none!important; padding:0px 0px 0px 4px!important; margin:0px!important; display:inline!important; border:0px!important; max-width:none!important; vertical-align:baseline!important <span style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb if
I had a better understanding of them. And for this project, I cant think of any other way to do this, if I could, I would do it - LOL<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<span style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb Please keep in mind that I am using VB 2005 so things like array.contains and array.exists doesnt work <img alt="" border="0" src="http://www.computerhelp.forum/x_images/images/smilies/frown.gif" title="Frown" style="vertical-align:middle; font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb <br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<span style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb TIA!<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<span style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb Joni
<pre class="prettyprint lang-vb Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FINfile As String = "m:testxy212tv.fin"
Dim THVfile As String = "M:testtest.thv"

If System.IO.File.Exists(finFile) = True Then

Dim FINrdr As StreamReader
FinRdr = File.OpenText(finFile)

Dim THVwrtr As StreamWriter
THVwrtr = System.IO.File.CreateText(THVfile)

Do While FinRdr.Peek() >= 0
Dim FINrec As String = FINrdr.ReadLine
Dim RespInfo As String = FINrec.Substring(0, 12)
Dim THVrec As String = ""
initialize indexes
Dim SCindex As Integer = 134
Dim HHCindex As Integer = 138
Dim i As Integer = 0
Dim j As Integer = 0

Dim StationType As Integer = 0

Dim RecLength As Integer = FINrec.Length()
Dim EndOfRec As Integer = RecLength - 3

Dim CableCount As Integer = 0 Counts total Cable half hours per respondent, no duplicates
Dim BroadcastCount As Integer = 0 Counts total Broadcast half hours per respondent, no duplicates
Dim CodeCNT As Integer = 0 Counts every single half hour per respondent, including duplicates
Dim DupCNT As Integer = 0
Dim TotalNoDupes As Integer = 0 Counts total number of 1/2 hour codes per respondent, no duplicates
Dim OtherCount As Integer = 0 Counts single half hours per repondent that are neither cable or broadcast

Switches to determine whether station is Broadcase, Cable or Other
Dim Broadcast As Boolean = False
Dim Cable As Boolean = False
Dim Other As Boolean = False

Dim ArrayDimension As Integer = (RecLength - 134) / 7
Dim myArray(ArrayDimension, 1) As Integer

Do Until HHCindex > EndOfRec
Dim StationCode As Integer = FINrec.Substring(SCindex, 4)
Dim HalfHourCode As Integer = FINrec.Substring(HHCindex, 3)

CodeCNT += 1

TV Ranges
Broadcast: 1000 – 7999, 9700, 9800
Cable: 8000 – 9000, 9900
Determine whether station is Cable, Broadcast or Other
If StationCode > 7999 And StationCode < 9001 Or StationCode = 9900 Then
Cable = True
StationType = 1
ElseIf StationCode > 999 And StationCode < 8000 Or StationCode = 9700 Or StationCode = 9800 Then
Broadcast = True
StationType = 2
Else
Other = True
StationType = 3
End If

Dim DupSW As Boolean = False
Dim x As Integer = 0
For x = 0 To i - 1
If myArray(x, 0) = HalfHourCode And myArray(x, 1) = StationType Then
DupCNT += 1
DupSW = True
Else
TotalNoDupes += 1
End If
Next x

If DupSW = False Then
If Broadcast = True Then
BroadcastCount += 1
ElseIf Cable = True Then
CableCount += 1
ElseIf Other = True Then
OtherCount += 1
End If
End If

myArray(i, j) = HalfHourCode
j += 1
myArray(i, j) = StationType
i += 1
j = 0


SCindex = SCindex + 7
HHCindex = HHCindex + 7

Broadcast = False
Cable = False
Other = False

Loop

Dim CodeCNTout As String = Format(CodeCNT, "000")
Dim TotalNoDupesOUT As String = Format(TotalNoDupes, "000")

If CodeCNTout = "" Then
CodeCNTout = "000"
End If

If TotalNoDupesOUT = "" Then
TotalNoDupesOUT = "000"
End If

THVrec = RespInfo & Space(17) & CodeCNTout & Space(2) & TotalNoDupesOUT

THVwrtr.WriteLine(THVrec)

Loop
FINrdr.Close()
Else
MsgBox("******** " & FINfile & " DOES NOT EXIST! ********")
Exit Sub
End If

MsgBox("complete")[/code]
<br/>
<span style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb <br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<br style="font-family:verdana,geneva,lucida,lucida grande,arial,helvetica,sans-serif; font-size:13px; line-height:normal; background-color:#fbfbfb
<br/>

<
-Joni :)

View the full article
 
Back
Top