Results 1 to 3 of 3

Thread: How to get the first soonest occurrence between a use of a List and Dictionary

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    73

    How to get the first soonest occurrence between a use of a List and Dictionary

    The list is unsorted with the values that come first second and third (100,200,300)
    The list indicates the order of the values which is first,second or third.
    The Row's are what I call the Keys of the Dictionary
    The Values of the dictionary are values that are used in the list

    (Since Row:200 has the smallest soonest Value:2) (based on newList) 2 is before 3.
    The Value:3 in Row:100 is later then Value:2, that's why the output should be Row:200.
    After Row:200 is resolved, the next output would be Row:300 and Row:100 is the last outputted value, but I only need output one result not all 3.

    Keys are Rows
    Values are Values

    Output is the Key of Dictionary

    Code:
            Public Module Program
                Public Sub Main(args() As String)
        
                    'This kinda works I guess
                    Dim newList2 As New List(Of Byte)({1, 2, 3})
                    Dim ListOfValues2 As New Dictionary(Of Integer, Byte)
                    ListOfValues2.Add(100, 3)
                    ListOfValues2.Add(200, 2)
                    ListOfValues2.Add(300, 1)
            
                Dim firstToProcessRow As Integer = 0
                For Each value In newList2
                    For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues2
                        If ListOfValues2.ContainsKey(Row.Value) AndAlso ListOfValues2(Row.Value) = value OrElse Row.Key = 0 Then
                            firstToProcessRow = Row.Key
                            GoTo exitFor
                        End If
                    Next
                Next
        exitFor:
    
                Debug.WriteLine(firstToProcessRow)
                'Output is 100 when it should be 200.
    
                End Sub
            End Module
    Last edited by pkedpker; Nov 8th, 2021 at 10:57 PM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: How to get the first soonest occurrence between a use of a List and Dictionary

    Did you step through the code?
    The code doesn't make any sense.
    I don't see how it could possibly be putting out 100. I would expect it to put out 0.

    Your dictionary contains the Keys 100, 200, 300.
    You are looking to see if it contains the Keys 3, 2, 1.
    Since it does not contain those keys it will never set firstToProcessRow to anything so it should remain equal to 0.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    73

    Re: How to get the first soonest occurrence between a use of a List and Dictionary

    Hey passel I resolved this question by using scoring, But it still came short in my project can you look into my project with the Solve button? I'm glad I finally got in contact with you, you are probably the only person who can help me lol

    here is how i solved this question
    Code:
           If newList.LongCount > 0 Then
                Dim scores As Integer() = New Integer(ListOfValues.Count - 1) {}
                Dim scoreCounter As Integer = 0
                Dim highestScore As Integer = -1
    
                For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues
                    Dim key As Integer = Array.IndexOf(newList.ToArray(), Row.Value)
                    scores(scoreCounter) = key
                    scoreCounter += 1
                    If key > highestScore Then highestScore = key
                Next
    
                Debug.WriteLine("scores")
                Debug.WriteLine("Highest Score: " & highestScore)
                If highestScore <> -1 Then
                    Dim firstLowestValue As Integer = highestScore
                    scoreCounter = 0
    
                    For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues
                        If scores(scoreCounter) <> -1 AndAlso scores(scoreCounter) < firstLowestValue Then
                            Debug.WriteLine("Lowest = " & newList(scores(scoreCounter)) & " Lowest Row = " & Row.Key)
                            Return Row.Key
                        End If
                        scoreCounter += 1
                    Next
                    firstLowestValue = highestScore
                    scoreCounter = 0
                    For Each Row As KeyValuePair(Of Integer, Byte) In ListOfValues
                        If scores(scoreCounter) <> -1 AndAlso scores(scoreCounter) <= firstLowestValue Then
                            Debug.WriteLine("Lowest2 = " & newList(scores(scoreCounter)) & " Lowest Row = " & Row.Key)
                            Return Row.Key
                        End If
                        scoreCounter += 1
                    Next
    
                    If firstLowestValue = highestScore Then Return ListOfValues.Keys.Min()
                End If
            Else
                Return CurrentRow
            End If
    Good Project.7z
    Name:  pic.jpg
Views: 176
Size:  37.7 KB

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width