[erlang-questions] Case-insensitive key search on a list

Tristan Sloughter <>
Mon Apr 30 19:54:19 CEST 2012


So you want to find a tuple in a list converted from JSON so the key is
binary. And not only that, you want it to be case insensitive!

Now as far as I found, I'm only now bothering to go back to this code and
ask why, there is only string:equal/2 and it doesn't support case
insensitive matching. And not only that but string:to_lower/1 only supports
lists. So I ended up with a conversion to a list, then to_lower and then
back to a binary....

It is too ugly to think it was right... Does anyone know a better way of
doing this? I feel I am missing something obvious.

case_insensitive_binary_string_keyfind(_String, _Pos, []) ->
    false;
case_insensitive_binary_string_keyfind(String, Pos, [H|T]) ->
    case list_to_binary(
           string:to_lower(
             binary_to_list(
               element(Pos, H)))) of
        String ->
            H;
        _ ->
            case_insensitive_binary_string_keyfind(String, Pos, T)
    end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120430/ff1edb82/attachment.html>


More information about the erlang-questions mailing list