[erlang-questions] How to match on a nested record against a dict

Igor Ribeiro Sucupira igorrs@REDACTED
Thu Nov 26 01:09:28 CET 2009


If you don't need the values that you store in the dict (in your
example, you store [] and never use it), you can use a sets instead of
a dict. Usage is a little bit simpler:

-------------------------
1> D = dict:store("_see._tcp.local", [], dict:new()).
{dict,1,16,16,8,80,48,
      {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
      {{[],[],[],[],
        [["_see._tcp.local"]],
        [],[],[],[],[],[],[],[],[],[],[]}}}
2> dict:find("_see._tcp.local", D).
{ok,[]}

3> S = sets:add_element("_see._tcp.local", sets:new()).
{set,1,16,16,8,80,48,
     {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
     {{[],[],[],[],
       ["_see._tcp.local"],
       [],[],[],[],[],[],[],[],[],[],[]}}}
4> sets:is_element("_see._tcp.local", S).
true
-------------------------


See also gb_sets and ordsets.

Igor.

On Tue, Nov 24, 2009 at 7:25 PM, Jarrod Roberson <jarrod@REDACTED> wrote:
> On Tue, Nov 24, 2009 at 3:18 PM, Jarrod Roberson <jarrod@REDACTED>wrote:
>
>>
>> process_dnsrec1(Sub,[#dns_rr{domain=Dom}|Rest]) ->
>>   case dict:find(Dom,Sub) of
>>       [Result] ->
>>           io:format("Interesting domain ~p=~p~n",[Dom,Result]);
>>
>
> thanks for reading I figured it out by trial and error.
> I replaced the [Result] with {ok,Result} and it started working as expected



-- 
"The secret of joy in work is contained in one word - excellence. To
know how to do something well is to enjoy it." - Pearl S. Buck.


More information about the erlang-questions mailing list