Reputation: 1976
I've been stumped just trying to compare two bits of data for hours.
This is essentially what i've written..
find_client(Search, [Client|Client_list])->
{Name,Socket} = Client,
io:fwrite("Name>~s<~n",[Name]),
io:fwrite("Search>~s<~n",[Search]),
case string:equal(Name,Search) of
true->
do_something;
false->
do_something_else
end;
find_client(Search,[])->
not_found.
The problem is that do_something_else is always returned even when i'm convinced they should be equal! io:fwrite prints out exactly the same thing to the console in my tests, ie-
Name>name1<
Search>name1<
Before I tried string:equal I was trying to do my own pattern match but no matter the combination I couldn't seem to get it to work.
Am I missing something? I'd really appreciate a pait of fresh eyes or a suggestion for another way to try.
Upvotes: 1
Views: 1597
Reputation: 2069
All of these print the same thing, because ~s formats iolists (and atoms!) the same way.
io:fwrite("~s~n", ["name"]),
io:fwrite("~s~n", [<<"name">>]),
io:fwrite("~s~n", [name]),
io:fwrite("~s~n", [[$n, $a, <<"m">>, "e"]]).
A better "what is this really?!" debug technique would be to use the ~p format string. Give it a try with the above examples.
The implementation of string:equal/2
isn't really any different than the =:=
operator (it's a pattern match), so it's not going to return true for equivalent iolists that have different structure.
What you'll probably need to do is write a comparison function that can compare iolists, which is trivially done using iolist_to_binary/1
.
iolist_equal(A, B) ->
iolist_to_binary(A) =:= iolist_to_binary(B).
Upvotes: 8