在每个列表项上搜索字符串

时间:2014-06-13 17:35:36

标签: string list erlang

我需要在List中搜索包含此字符串的项目:sp1_inicio.pbf

列表的格式为:

["X,sp1_inicio.pbf,2,AB5E","X,sp1_chile.pbf,3,4F46"]

应用程序名称是第二个元素sp1_inicio.pbf,版本是第三个元素2

应用程序名称是唯一的,无论列表有多大,都不会重复。

因此,基于第一个字符串,我需要在此列表中搜索正确的应用程序并获取其版本号。

这些数据是从Riak返回的,在下面的代码中,我只展示了我为处理这种情况而创建的方法。

这是我的代码,我获取列表和文件名(不要期望太多,这是我的第一个代码):

get_application_version(LogicalNumber, Acronym, Object) ->
    {_, Where, Name, _, _, _, _} = Object,
    {ok, Client} = riak:local_client(),
    % Try to get the logical number from the bucket terminals
    case Client:get(<<"terminals">>, LogicalNumber) of
        % If the logical number is returned, its value goes to the Terminal Variable
        {ok, Terminal} ->
            % Returns the Terminal value, in this case it is a json: group: xxx and decode it
            {struct, TerminalValues} = mochijson2:decode(riak_object:get_value(Terminal)),
            % Use proplist to get the value of the decoded json key 'groups'
            % Here we already have the group ID of the current logical number in the variable GroupID
            GroupId = proplists:get_value(<<"group">>, TerminalValues),
            % Acronym with _
            Acronym_ = string:concat(binary_to_list(Acronym), "_"),
            % Group with acronym ex.: ab1_123
            GroupName = string:concat(Acronym_, binary_to_list(GroupId)),
            case Client:get(<<"groups">>, list_to_binary(GroupName)) of
                {ok, Group} ->
                    {struct, GroupValues} = mochijson2:decode(riak_object:get_value(Group)),
                    AppsList = proplists:get_value(<<"apps_list">>, GroupValues);
                    %%% Right here I have all the data required to make the list search
                    %%% The list is inside AppsList
                    %%% The application name is inside Name
                {error, notfound} ->
                    io:format("Group notfound")
            end;
        {error, notfound} ->
            io:format("Terminal notfound")
    end.

我不知道创建一个包含字符串的列表是否是最好的方法,或者即使这是一种禁食方法而且让我担心。

1 个答案:

答案 0 :(得分:2)

您可以使用例如以下代码:

find_app(Name, AppsList) ->
    F = fun(X) ->
                case string:tokens(X, ",") of
                    [_, Name, Version|_] -> {ok, Version};
                    _ -> next
                end
        end,
    find_first(F, AppsList).

bin_find_app(Name, AppsList) ->
    F = fun(X) ->
                case binary:split(X, <<$,>>, [global]) of
                    [_, Name, Version|_] -> {ok, Version};
                    _ -> next
                end
        end,
    find_first(F, AppsList).

find_first(_, []) -> not_found;
find_first(F, [X|L]) ->
    case F(X) of
        next -> find_first(F, L);
        Result -> Result
    end.

使用示例:

1> c(search_for).
{ok,search_for}
2> L = ["X,sp1_inicio.pbf,2,AB5E","X,sp1_chile.pbf,3,4F46"].
["X,sp1_inicio.pbf,2,AB5E","X,sp1_chile.pbf,3,4F46"]
3> Name = "sp1_inicio.pbf".
"sp1_inicio.pbf"
4> search_for:find_app(Name, L).
{ok,"2"}
5> search_for:bin_find_app(list_to_binary(Name), [list_to_binary(X) || X <- L]).
{ok,<<"2">>}

修改:您也可以使用二进制文件。