Zochonis
Zochonis

Reputation: 81

How to parse for integers in individual elements in an array

I want to be able to read from an array a specific set of lines that relate to a certain name I have in another array

for example:

in the names array i have "Ben" stored as the name and i want to see if the other array also contains the name "Ben" and if it does the it will add the scores of each line where "Ben" is mentioned

other array:

"Ben got 5" "Nash got 6" "Ben got 4" "Josh got 1"

so it will only add 5 and 4 to get 9

the program will then save the calculated number to a list.

For Each n As String In commonNames 'for the example the commonNames array contains Ben"
                If names.Contains(n) Then
                    'add each of their scores and divide by how many their are
                End If
            Next
            Console.ReadLine()
            For Each n As String In originames
                'add each score to the user and divide by 1

any help would be appreciated guys :)

Upvotes: 0

Views: 83

Answers (1)

Dim data = {"Ben got 5", "Nash got 6", "Ben got 4", "Josh got 1", "Ziggy got 42"}
Dim names = {"Ben", "Ziggy"}
Dim results(names.Length - 1) As Int32

For Each item In data
    For n As Int32 = 0 To names.Length - 1
        ' see if this item starts with name data
        If item.StartsWith(names(n)) Then
            ' if so, parse out the value
            Dim parts = item.Split(" "c)
            results(n) += Int32.Parse(parts(parts.Length - 1))
            Exit For
        End If
    Next
Next

' show contents of parallel arrays:
For n As Int32 = 0 To names.Length - 1
    Console.WriteLine("{0} total = {1}", names(n), results(n))
Next

Result:

Ben total = 9
Ziggy total = 42

If the data might include non numerals at the end, use TryParse insteasd.


Based on the series of related questions, you should seriously consider using some classes which would allow you to store related data together. Rather than the name in one array and the score/count in another, a class helps keep everything together. See: Five Minute Intro To Classes and Lists in this answer.

Using the simple NameValuePair utility class from this answer the code actually becomes simpler (one loop only) and the name and count stay together:

Dim np As NameValuePair                       ' scratch var
Dim players As New List(Of NameValuePair)     ' list

' add the names you are looking for to
' the list with 0 Score/Count
' not needed if they are in the list from code upstream
For Each n As String In names
    players.Add(New NameValuePair(n, 0))
Next

For Each item In data
    Dim parts = item.Split(" "c)
    ' is there a player name for this one?
    np = players.FirstOrDefault(Function(w) w.Name = parts(0))

    If np IsNot Nothing Then
        np.Value += Int32.Parse(parts(parts.Length - 1))
    End If
Next

' the data is together: print it
For Each np In players
    Console.WriteLine("Name: {0} has {1} apples", np.Name, np.Value)
Next

Upvotes: 1

Related Questions