2014-06-13 3 views
1

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. 

는 나도 몰라 이것을하는 가장 좋은 방법은 단식적인 접근 방법이고 그것이 나를 걱정하는 것입니다.

+0

죄송합니다. 귀하의 질문에 이해가되지 않습니다. 목록에 'sp1_inicio.posxml'이 보이지 않습니다. 따라서 답을 얻으려는 경우 입력 및 원하는 출력 예제를 제공해주십시오. 데이터가 어디서오고 어떤 것을 달성하고 싶은지 더 많은 정보를 제공하면 도움이 될 것입니다. –

+0

@ Hynek-Pichi-Vychodil은 많은 의견을 보내 주셔서 감사합니다. 지금 당장 문제를 개선하고 오타로 인해 유감스럽게 생각합니다. – Gerep

+0

@ Hynek-Pichi-Vychodil 누락 된 것이 있으면 질문을 업데이트했습니다. =) – Gerep

답변

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">>} 

편집을 : 당신은뿐만 아니라 바이너리와 함께 작업 할 수 있습니다.

+0

답장을 보내 주셔서 감사합니다. – Gerep

+0

완벽하게 작동합니다. 시간과 노력에 감사드립니다. – Gerep