Laxmikant Ratnaparkhi
Laxmikant Ratnaparkhi

Reputation: 5013

Search a key erlang

I have a form list in a tuple in the following format and a current page
CurrPageName = "ihtx_f_10_00_00_00_00_h210401".

FormList = {form_list, [{"IHTX_F_10_00_00_00_00_H210401",true},
                        {"IHTX_F_09_00_00_00_00_H210401",true},
                        {"IHTX_F_11_11_02_02_01_H220401",true},
                        {"IHTX_F_03_00_00_00_00_H210401",false},
                        {"IHTX_F_12_00_00_00_00_H211215",true},
                        {"IHTX_F_07_00_00_00_00_H210401",true},
                        {"IHTX_F_15_00_00_00_00_H210401",false},
                        {"IHTX_F_11_00_00_00_00_H210401",false},
                        {"IHTX_F_02_00_00_00_00_H210401",true},
                        {"IHTX_F_01_00_00_00_00_H240401",true}]}.

How to find CurrPageName from the FormList? I tried lists:keyfind, keysearch to the innerlist but always returning false or some error. If the CurrPageName exist and if its value is true then only it should return a true else false.

I'm newbie to erlang. Thanks

Upvotes: 0

Views: 133

Answers (2)

BlackMamba
BlackMamba

Reputation: 10252

you can use proplists.

CurrPageName = string:to_upper("ihtx_f_10_00_00_00_00_h210401").
{form_list, L} = FormList, 
Res = proplists:is_defined(CurrPageName, L)

Upvotes: 1

johlo
johlo

Reputation: 5500

The list stores the page names as upper-case strings, so first make sure your CurrPageName variable contains upper-case string too

CurrPageName = string:to_upper("ihtx_f_10_00_00_00_00_h210401").

Then extract the list of tuples and search for the page

{form_list, L} = FormList, % Extract tuple list to L
KeyPosition=1,             % The name we look for is at position 1 in the tuple
T=lists:keyfind(CurrPageName, KeyPosition, L),
case T of
 {_Key, true} ->
    true ;
 false ->
    false
end.

Upvotes: 2

Related Questions