[erlang-questions] Processing a List into a Dict

Gleb Peregud gleber.p@REDACTED
Mon Nov 30 22:12:57 CET 2009


1> X = ["txtvers=1","userid=3A6524D4-E31C-491D-94DD-555883B1600A","name=Jarrod
Roberson","version=2"].
2> dict:from_list([list_to_tuple(string:tokens(T,"=")) || T <- X]).
{dict,4,16,16,8,80,48,
      {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
      {{[],[],[],[],
        [["name",74,97,114,114,111,100,10,82,111,98,101,114|...]],
        [],[],
        [["txtvers",49]],
        [],[],[],
        [["userid",51,65,54,53,50|...]],
        [],
        [["version",50]],
        [],[]}}}

On Mon, Nov 30, 2009 at 22:06, Jarrod Roberson <jarrod@REDACTED> wrote:
> I have the following list
>
> ["txtvers=1","userid=3A6524D4-E31C-491D-94DD-555883B1600A","name=Jarrod
> Roberson","version=2"]
>
> I want to create a Dict where the left side of the = is the key and the
> right side is the value.
> Preferably where the key is an atom.
>
> Using the following list comprehension I get this.
>
>  KVL = [string:tokens(T,"=") || T <- TXT].
>
> [["txtvers","1"],
> ["userid","3A6524D4-E31C-491D-94DD-555883B1600A"], ["name","Jarrod
> Roberson"], ["version","2"]]
>
> what I am struggling with now is how to convert the nested lists into tuples
> so I can send them into a list of tuples
> where I can send them into dict:from_list
>
> what I want is something like this
>
> [{txtvers,"1"}, {userid,"3A6524D4-E31C-491D-94DD-555883B1600A"},
> {name,"Jarrod Roberson"}, {version,"2"}]
>
> I know there has to be a concise way to do this but I just can't get my head
> around it.
>


More information about the erlang-questions mailing list