Erlang, extract hashtag

184 Views Asked by At

Im trying to extract a hashtag out of a list that im retrieving when using the twiter api.

I use this to extract the hashtag:

case extract(<<"entities">>, L) of 
   {found, {TE}} ->
       {found, Hashtags} = extract(<<"hashtags">>, TE);
   not_found -> Hashtags = hash_not_found
   end,

extract(K, L) ->
 case lists:keyfind(K, 1, L) of
   {_, M} -> {found, M};
   false  -> not_found
 end.

This gives me a hashtag in the format:

[{[{<<"text">>,<<"London">>},{<<"indices">>,[120,127]}]}]

I wish to extract the hashtag only out of that, which would be London in this case. This tag will differ each time I extract tweets though. Any idea or suggestion is appreciated.

I do not wish to change earlier code unless I really have to. Id prefer to learn how to extract what I need from that list.

1

There are 1 best solutions below

3
Alexey Romanov On

Guessing at what extract function does, something like this should work:

case extract(<<"hashtags">>, TE) of
  {found, Hashtags} ->
    case extract(<<"text">>, Hashtags) of 
      {found, HashtagTexts} -> HashtagTexts;
      not_found -> hash_not_found
    end;
  not_found -> hash_not_found
end

See also proplists module in the standard library.