Skip to main content

Read 2 column comma delimited text into dictionary

Comments

1 comment

  • Berend Veldkamp

    If you already have the file contents in a string variable you could use this code in an Assign activity (add a reference to the System.Linq assembly). The result is a Dictionary(Of String, String). No checking is done for lines containing only a single value, or unique key values.

     

    completeFile.Split({Environment.NewLine}, StringSplitOptions.None).Select(Function(s) s.Split(","c)).ToDictionary(Function(tokens) tokens(0), Function(tokens) tokens(1))

     

    Unreadable indeed, but what it basically does is loop over each line, split on a comma and add to the dictionary. Here's a more verbose piece of code:

    For Each line In completeFile.Split({Environment.NewLine}, StringSplitOptions.None)

     

        Dim tokens = line.Split(","c)

     

        dict.Add(tokens(0), tokens(1))

     

    Next

     

    It is arguable wether the 1st method is better/more efficient than other methods, but you only need one activity to create and assign the dictionary, which is what I prefer myself.

     

    0

Please sign in to leave a comment.