Populate dictionary with text file VB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GTXY20
    New Member
    • Oct 2007
    • 29

    Populate dictionary with text file VB

    Hi All,

    I have been able to create a successful application with Python but I am being asked to create in VB.

    I have a text file with the following data:

    1,a
    1,b
    1,c
    2,a
    2,a
    2,b
    3,a
    3,c

    I need to create a dictionary in VB so that I can then iterate over it so that my key is the first field and my item is the concatenation of unique items for each key in the text file:

    In Python I would have:

    Code:
    {1:(a,b,c), 2:(a,b), 3:(a,c)}
    I am starting with the following in VB:

    Code:
    Private Sub ImpFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImpFile.Click
            Dim uhdict As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
            Dim openimp As New OpenFileDialog()
            If openimp.ShowDialog() = DialogResult.OK Then
                Dim sr As New System.IO.StreamReader(openimp.FileName)
                sr.Close()
            End If
        End Sub
    How do I begin to assign a variable for the key, assign a variable for the items and then add to the dictionary uhdict.

    Any help and guidance wher to start is very much appreciated.

    Thanks again.
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Originally posted by GTXY20
    Hi All,

    I have been able to create a successful application with Python but I am being asked to create in VB.

    I have a text file with the following data:

    1,a
    1,b
    1,c
    2,a
    2,a
    2,b
    3,a
    3,c

    I need to create a dictionary in VB so that I can then iterate over it so that my key is the first field and my item is the concatenation of unique items for each key in the text file:

    In Python I would have:

    Code:
    {1:(a,b,c), 2:(a,b), 3:(a,c)}
    I am starting with the following in VB:

    Code:
    Private Sub ImpFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImpFile.Click
            Dim uhdict As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
            Dim openimp As New OpenFileDialog()
            If openimp.ShowDialog() = DialogResult.OK Then
                Dim sr As New System.IO.StreamReader(openimp.FileName)
                sr.Close()
            End If
        End Sub
    How do I begin to assign a variable for the key, assign a variable for the items and then add to the dictionary uhdict.

    Any help and guidance wher to start is very much appreciated.

    Thanks again.
    Are you having issues with reading the text file and extracting its values or with adding elements into the dictionary? Or all of it?
    Dictionary has an Add method

    [CODE=cpp]int x = 0;//put your number here
    string val = "value";//
    uhdict.Add(x,va l);[/CODE] (Do apply the conversion to vb .net from one of the online converters)

    Comment

    • GTXY20
      New Member
      • Oct 2007
      • 29

      #3
      Thanks but I am having difficulty assigning the variables to the text file so that if I use split by "," the first variable say a is assigned that value and that will be the key and the next value after the comma is assigned to say variable b and it will be the value for the key - I then need to append to the values where the key is the same when reading the text file and splitting using comma. Something like:

      1,a
      1,b
      1.c


      Key = 1

      Value = a,b,c

      So can I use a for loop to say if Key is present append value if it doesn't currently exist and sort values.

      Thanks.

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        Originally posted by GTXY20
        Thanks but I am having difficulty assigning the variables to the text file so that if I use split by "," the first variable say a is assigned that value and that will be the key and the next value after the comma is assigned to say variable b and it will be the value for the key - I then need to append to the values where the key is the same when reading the text file and splitting using comma. Something like:

        1,a
        1,b
        1.c


        Key = 1

        Value = a,b,c

        So can I use a for loop to say if Key is present append value if it doesn't currently exist and sort values.

        Thanks.
        Hi ,
        PLease have a look at some tutorials on how to use a Dictionary.
        By the way you speak, I dont think you have looked at this datattype.

        good luck

        Comment

        Working...