[erlang-questions] Processing a List into a Dict

Jarrod Roberson jarrod@REDACTED
Mon Nov 30 22:22:23 CET 2009


Thanks for the responses, that got me on the right track, here is what I
came up with.

TXT =
["txtvers=1","userid=3A6524D4-E31C-491D-94DD-555883B1600A","name=Jarrod
Roberson","version=2"].

A = [ string:tokens(KV,"=") || KV <- TXT].
[["txtvers","1"],["userid","3A6524D4-E31C-491D-94DD-555883B1600A"],["name","Jarrod
Roberson"],["version","2"]]

D = [{list_to_atom(K),V} || [K|[V|_]] <- A].
[{txtvers,"1"},{userid,"3A6524D4-E31C-491D-94DD-555883B1600A"},{name,"Jarrod
Roberson"},{version,"2"}]

DT = dict:from_list(D).
{dict,4,16,16,8,80,48,
      {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
      {{[],[],[],
        [[txtvers,49]],
        [[userid,51,65,54,53,50,52,68,52,45,69,51,49|...]],
        [[name,74,97,114,114,111,100,32,82,111,98,101|...]],
        [],[],[],[],[],[],[],[],
        [[version,50]],
        []}}}

dict:find(name,DT).
{ok,"Jarrod Roberson"}

this gets me the keys as atoms instead of "strings"


More information about the erlang-questions mailing list