0

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 :)

Zochonis
  • 81
  • 3
  • 11
  • This is a [continuation](http://stackoverflow.com/questions/35786753/regex-to-extract-names-from-a-string-into-a-list), is it? ;) – Ian Mar 04 '16 at 04:43
  • yeah, am basically seeing if one array contains the same name as another array and comparing it to the elements in the other array and every time "Ben" is mentioned in a line the scores on that line are added together – Zochonis Mar 04 '16 at 04:48
  • I think it is best to have a class describing your object here, something like `Person` – Ian Mar 04 '16 at 05:21

1 Answers1

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
Community
  • 1
  • 1
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • thx pluto! but instead of storing them to an array how could you store them to a list? – Zochonis Mar 04 '16 at 18:15
  • ***I*** would leave it as it so that the values are in the same order as the names, then do: `Dim resultList As New List(of Int32)(results)` otherwise you can end up with the names unrelatable to the score. But as stated a nice modern OOP class would work better for whatever you are doing - way too many parallel arrays. [A simple NameValuePair](http://stackoverflow.com/a/31811533/1070452) might work to keep the name with the score/count – Ňɏssa Pøngjǣrdenlarp Mar 04 '16 at 18:20