Reputation: 65
I am trying to print the keys by comparing the values of a map with the given input as string (input is given in the below tried executed sample output).
See the below code I tried code:
-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).
When I executed the code , the output is below:
**
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
**
Please suggest me ?
Upvotes: 1
Views: 181
Reputation: 1958
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" => "--.."}.
Upvotes: 2