Reputation: 67
I have the two textboxes:
First:
Textbox1.lines(0) = 50
Textbox1.lines(1) = 65
Textbox1.lines(2) = 41
Textbox1.lines(3) = 27
Textbox1.lines(4) = 6
Textbox1.lines(5) = 6
Second:
Textbox2.lines(0) = 27
Textbox2.lines(1) = 41
Textbox2.lines(2) = 65
Textbox2.lines(3) = 6
Textbox2.lines(4) = 50
Textbox2.lines(5) = 6
in a third textbox I should display the index that contains the values from the first textbox, but in the second.
Textbox3.lines(0) = 4 (50 of the first textbox is on the second line (lines4)
Textbox3.lines(1) = 2 (65 of the first textbox is on the second line (lines2)
Textbox3.lines(2) = 1 (41 of the first textbox is on the second line (lines1)
Textbox3.lines(3) = 0 (27 of the first textbox is on the second line (lines0)
Textbox3.lines(4) = 3 (6 of the first textbox is on the second line (lines4)
Textbox3.lines(5) = 5 (6 of the first textbox is on the second line (lines5)
although it already exists on line 4 (Number 6), we will move next line, because that line has already been considered. or both index can be displayed. or somehow the line value becomes null (0) so that it is not taken.
Code: it doesn't work properly, unfortunately.
For Each line In TextBox1.Lines
For l As Integer = 1 To TextBox1.Lines.Length - 1
If TextBox2.Lines(l) = line Then
TextBox3.AppendText(l)
End If
Next
Next
Upvotes: 1
Views: 67
Reputation: 67
I managed to do that as well with the help of a list. In order to work, the number of lines must be equal.
Dim valI As New List(Of Integer)
For nIndex As Integer = 0 To Textbox2.Lines.Length -1
For i As Integer = 0 To TextBox1.Lines.Length - 1
If TextBox2.Lines(nIndex) = TextBox1.Lines(i) Then
If valI.Contains(i) Then
Else
valI.Add(i)
End If
End If
Next
Textbox3.AppendText(vbNewline & valI.Item(nIndex))
Next
Upvotes: 0
Reputation: 655
This should work :
For Each line In TextBox1.Lines
Dim i As Integer = 0
While (i < TextBox1.Lines.Length)
If TextBox2.Lines(i) = line Then
TextBox3.AppendText(i & Environment.NewLine)
Continue For
End If
i += 1
End While
Next
With Continue For, the code go to the for's next loop.
Or if you want to display all iterations :
For Each line In TextBox1.Lines
Dim i As Integer = 0
While (i < TextBox1.Lines.Length)
If TextBox2.Lines(i) = line Then
TextBox3.AppendText(i & " ")
End If
i += 1
End While
TextBox3.AppendText(Environment.NewLine)
Next
Upvotes: 2
Reputation: 39142
This approach uses a Dictionary with the number as the key, and a Queue of indices as the value.
When we first encounter a number from TextBox1, we build a queue of all indices where the number occurs in TextBox2. Then each time we encounter that number, we dequeue the next available number where it occurred. If there are none left, then we return -1.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim results As New List(Of String)
Dim occurrences As New Dictionary(Of Integer, Queue(Of Integer))
For Each number As String In TextBox1.Lines
If Not occurrences.ContainsKey(number) Then
occurrences.Add(number, New Queue(Of Integer))
For i As Integer = 0 To TextBox2.Lines.Count - 1
If TextBox2.Lines(i) = number Then
occurrences(number).Enqueue(i)
End If
Next
End If
If occurrences(number).Count > 0 Then
results.Add(occurrences(number).Dequeue)
Else
results.Add("-1")
End If
Next
TextBox3.Lines = results.ToArray
End Sub
Upvotes: 1