I’m using: SWI-Prolog version 9.0.4
I want the code to: get an assoc from a string
But what I’m getting is: It doesn’t seem to work running like this:
run(“one:two:=three”).
But when I hardcode MatchedWords instead of getting it from the regular expression, it works!
I am a beginner with Prolog and maybe the solution is simple but I don’t see it!
My code looks like this:
:- module(test_ofs_out, [run/1]).
:- autoload(library(lists), [append/3, last/2]).
:- autoload(library(pcre), [re_foldl/6]).
:- autoload(library(assoc), [list_to_assoc/2, get_assoc/3]).
run(OfsMsg) :-
split_string(OfsMsg, ",", " ", OfsMsgDataList),
format("OfsMsgDataList: ~w~n", [OfsMsgDataList]),
ofs_data_to_kv_list(OfsMsgDataList, KVList),
format("KVList: ~w~n", [KVList]),
list_to_assoc(KVList, Assoc),
format("Assoc: ~w~n", [Assoc]),
get_assoc(one, Assoc, Value),
format("Value: ~w~n", [Value]).
ofs_data_to_kv_list(OfsMsgDataList, KVList) :-
words_to_key_value(OfsMsgDataList, [], KVList).
words_to_key_value([], Acc, Acc).
words_to_key_value([Words | RemainingWords], Acc, KVList) :-
% MatchedWords = [one, two, three],
re_foldl(append_matched_word, "[a-z]+", Words, [], MatchedWords, []),
format("MatchedWords: ~w~n", [MatchedWords]),
[K | _] = MatchedWords,
last(MatchedWords, V),
append(Acc, [K-V], NewAcc),
words_to_key_value(RemainingWords, NewAcc, KVList).
append_matched_word(Elem, Acc, Result) :-
E = Elem.get(0),
append(Acc, [E], Result).