erlang 通过比较Map中的值来检索键

zwghvu4y  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(142)

我尝试通过比较map的值和给定的字符串输入来打印键(输入在下面尝试执行的示例输出中给出)。
看下面的代码我试过的代码:

-module(main).
-compile(export_all).
 get_code_for_words(Sentence) ->
   Words = string:split(Sentence, "\s", all),
   LettersWords = # { "a" => ".- ","b" =>"-... ","c" =>"-.-. ","d" => "-.. ","e" => ". ","f" => "..-. ","g" => "--. ","h" =>".... ","i" =>".. ","j" =>".--- ","k" =>"-.- ","l" =>".-.. ","m" =>"-- ","n" =>"-. ","o" => "--- ","p" =>".--. ","q" =>"--.- ","r" =>".-. ","s" =>"... ","t" =>"- ","u" =>"..- ","v" =>"...- ","w" =>".-- ","x" =>"-..- ","y" =>"-.--","z" =>"--.. "},
   Result = get_codes(Words, LettersWords, _AllCodes=[]),
   io:format("~p~n", [Result]).
            
 get_codes([Word|Words], Map, AllCodes) ->
    NewAllCodes = maps:fold(fun(K,V,Acc) ->
                           %io:format("~p~n",[Acc]),
                           case V =:= Word of
                                true -> [K|Acc];
                                _    -> Acc
                           end
                       end,
                       AllCodes,
                       Map),
            get_codes(Words, Map, NewAllCodes);
        get_codes([], _Map, AllCodes) ->
            lists:reverse(AllCodes).

当我执行代码时,输出如下:

**

9> c(main).                           
    main.erl:2: Warning: export_all flag enabled - all functions will be exported
    {ok,main}
    10> main:get_code_for_words(".... ..").
    []
    ok

**

请指点我好吗?

xsuvu9jc

xsuvu9jc1#

Let us first reformat your code (with the help of rebar3_format ):

get_code_for_words(Sentence) ->
    Words = string:split(Sentence, "\s", all),
    LettersWords =
        #{"a" => ".- ",
          "b" => "-... ",
          "c" => "-.-. ",
          "d" => "-.. ",
          "e" => ". ",
          "f" => "..-. ",
          "g" => "--. ",
          "h" => ".... ",
          "i" => ".. ",
          "j" => ".--- ",
          "k" => "-.- ",
          "l" => ".-.. ",
          "m" => "-- ",
          "n" => "-. ",
          "o" => "--- ",
          "p" => ".--. ",
          "q" => "--.- ",
          "r" => ".-. ",
          "s" => "... ",
          "t" => "- ",
          "u" => "..- ",
          "v" => "...- ",
          "w" => ".-- ",
          "x" => "-..- ",
          "y" => "-.--",
          "z" => "--.. "},
    Result = get_codes(Words, LettersWords, _AllCodes = []),
    io:format("~p~n", [Result]).

get_codes([Word | Words], Map, AllCodes) ->
    NewAllCodes =
        maps:fold(fun(K, V, Acc) ->
                     case V =:= Word of
                         true -> [K | Acc];
                         _ -> Acc
                     end
                  end,
                  AllCodes,
                  Map),
    get_codes(Words, Map, NewAllCodes);
get_codes([], _Map, AllCodes) ->
    lists:reverse(AllCodes).

Now, let's isolate the map so I don't have to copy it around all the time and, since this doesn't look like an escript, let's just remove the call to io:format/2 for the result…

letters_to_words() ->
        #{"a" => ".- ",
…
          "z" => "--.. "}.

get_code_for_words(Sentence) ->
    Words = string:split(Sentence, "\s", all),
    get_codes(Words, letters_to_words(), _AllCodes = []).

Now, let's talk about get_codes/3 … Since that function is using the map above, I'll assume that you when you say Word , you actually mean Character (since otherwise you'll have to go char-by-char, but this function goes word-by-word)…
Let's first use pattern-matching instead of a boolean case statement…

get_codes([Word | Words], Map, AllCodes) ->
    NewAllCodes =
        maps:fold(fun(K, V, Acc) ->
                     case V of
                         Word -> [K | Acc];
                         _ -> Acc
                     end
                  end,
                  AllCodes,
                  Map),
    get_codes(Words, Map, NewAllCodes);
get_codes([], _Map, AllCodes) ->
    lists:reverse(AllCodes).

Now, I'll assume that you expect this to happen…

8> a_module:get_code_for_words(".- -.-.").
["a","c"]

But, instead, you get…

8> a_module:get_code_for_words(".- -.-.").
[]

And, to understand why that happens, let's see what's the result of string:split/3 in your case…

9> string:split(".- -.-.", "\s", all).    
[".-","-.-."]

As you can see, those strings have no trailing spaces, like the keys on your map. So… replacing your map with the following one should fix your issue…

letters_to_words() ->
        #{"a" => ".-",
          "b" => "-...",
          "c" => "-.-.",
          "d" => "-..",
          "e" => ".",
          "f" => "..-.",
          "g" => "--.",
          "h" => "....",
          "i" => "..",
          "j" => ".---",
          "k" => "-.-",
          "l" => ".-..",
          "m" => "--",
          "n" => "-.",
          "o" => "---",
          "p" => ".--.",
          "q" => "--.-",
          "r" => ".-.",
          "s" => "...",
          "t" => "-",
          "u" => "..-",
          "v" => "...-",
          "w" => ".--",
          "x" => "-..-",
          "y" => "-.--",
          "z" => "--.."}.

相关问题