locked
search item in array RRS feed

  • Question

  • I want to add values from an iterative loop to a matrix and then re-search within the array

    I tried this code and did not work with me

      Dim myArray() As Integer
            Dim i As Integer
            For i = 0 To 20
                ReDim myArray(i & "bvs ")
            Next
            MsgBox(Array.IndexOf(myArray, "3"))

    Tuesday, October 31, 2017 4:33 PM

Answers

  • IndexOf is for an exact match. What do you want, an exact match or partial match?

    Edit: If you want to do a partial match within an array item here is an example.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' build an array
        Dim values As String() = Enumerable.Range(1, 10) _
        .Select(Function(item)
                    If item = 2 Then
                        Return $"Bo2o"
                    Else
                        Return $"item{item}"
                    End If
                End Function).ToArray
    
        Dim searchToken As String = "o2"
    
        ' use an anonymous type to locate searchToken
        Dim result = values.Select(Function(value, index) New With {.Value = value, .Index = index}) _
        .FirstOrDefault(Function(item) item.Value.Contains(searchToken))
    
        If result IsNot Nothing Then
            Console.WriteLine($"index: {result.Index} value: {result.Value} values: {values(result.Index)}")
        End If
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by KareninstructorMVP Tuesday, October 31, 2017 5:33 PM Added code sample
    • Marked as answer by ahmeddc Tuesday, October 31, 2017 6:07 PM
    Tuesday, October 31, 2017 4:38 PM
  • I want to add values from an iterative loop to a matrix and then re-search within the array

    I tried this code and did not work with me

      Dim myArray() As Integer
            Dim i As Integer
            For i = 0 To 20
                ReDim myArray(i & "bvs ")
            Next
            MsgBox(Array.IndexOf(myArray, "3"))

    You might want to use a generic List(Of YourTypeHere) and then use the .Contains method:

    https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/bhkz42b3(v=vs.110).aspx

    ... I hit Enter too soon.

    Use the contains method to verify that the item is in the list then use the .FindAll method to get that element.

    Example:

    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim stringList As New List(Of String) _
                From {"Dog", "Cow", "Pig"}
    
            If stringList.Contains("Cow") Then
                Dim foundElement As List(Of String) = _
                    stringList.FindAll(Function(animal) animal = "Cow")
    
                If foundElement IsNot Nothing AndAlso foundElement.Count = 1 Then
                    MessageBox.Show(foundElement.First)
                End If
            End If
    
            Stop
    
        End Sub
    End Class

    Depending on what you ultimately want to do here, a class might be a better option.


    "A problem well stated is a problem half solved.” - Charles F. Kettering



    • Edited by Frank L. Smith Tuesday, October 31, 2017 4:55 PM ...added example
    • Proposed as answer by Cor Ligthert Tuesday, October 31, 2017 5:07 PM
    • Marked as answer by ahmeddc Tuesday, October 31, 2017 6:07 PM
    Tuesday, October 31, 2017 4:42 PM

All replies

  • IndexOf is for an exact match. What do you want, an exact match or partial match?

    Edit: If you want to do a partial match within an array item here is an example.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' build an array
        Dim values As String() = Enumerable.Range(1, 10) _
        .Select(Function(item)
                    If item = 2 Then
                        Return $"Bo2o"
                    Else
                        Return $"item{item}"
                    End If
                End Function).ToArray
    
        Dim searchToken As String = "o2"
    
        ' use an anonymous type to locate searchToken
        Dim result = values.Select(Function(value, index) New With {.Value = value, .Index = index}) _
        .FirstOrDefault(Function(item) item.Value.Contains(searchToken))
    
        If result IsNot Nothing Then
            Console.WriteLine($"index: {result.Index} value: {result.Value} values: {values(result.Index)}")
        End If
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by KareninstructorMVP Tuesday, October 31, 2017 5:33 PM Added code sample
    • Marked as answer by ahmeddc Tuesday, October 31, 2017 6:07 PM
    Tuesday, October 31, 2017 4:38 PM
  • IndexOf is for an exact match


    • Edited by ahmeddc Tuesday, October 31, 2017 4:40 PM
    Tuesday, October 31, 2017 4:39 PM
  • I want to add values from an iterative loop to a matrix and then re-search within the array

    I tried this code and did not work with me

      Dim myArray() As Integer
            Dim i As Integer
            For i = 0 To 20
                ReDim myArray(i & "bvs ")
            Next
            MsgBox(Array.IndexOf(myArray, "3"))

    You might want to use a generic List(Of YourTypeHere) and then use the .Contains method:

    https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/bhkz42b3(v=vs.110).aspx

    ... I hit Enter too soon.

    Use the contains method to verify that the item is in the list then use the .FindAll method to get that element.

    Example:

    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim stringList As New List(Of String) _
                From {"Dog", "Cow", "Pig"}
    
            If stringList.Contains("Cow") Then
                Dim foundElement As List(Of String) = _
                    stringList.FindAll(Function(animal) animal = "Cow")
    
                If foundElement IsNot Nothing AndAlso foundElement.Count = 1 Then
                    MessageBox.Show(foundElement.First)
                End If
            End If
    
            Stop
    
        End Sub
    End Class

    Depending on what you ultimately want to do here, a class might be a better option.


    "A problem well stated is a problem half solved.” - Charles F. Kettering



    • Edited by Frank L. Smith Tuesday, October 31, 2017 4:55 PM ...added example
    • Proposed as answer by Cor Ligthert Tuesday, October 31, 2017 5:07 PM
    • Marked as answer by ahmeddc Tuesday, October 31, 2017 6:07 PM
    Tuesday, October 31, 2017 4:42 PM