small erlang question.

Peter Lund <>
Thu Jan 15 23:29:55 CET 2004


But better is:

case my_str(Text,[Str1,Str2,Str3]) of
   1 -> do_1();
   2 -> do_2();
   3 -> do_3();
   none -> ok
end

where:

my_str(Text,L) -> my_str(Text,L,1).
my_str(Text,[],N) -> none;
my_str(Text,[Str|T],N) ->
   case string:str(Text,Str) of
     0 -> my_str(Text,T,N+1);
     _ -> N
   end.

But that was maybe about what you did yourself...

> case string:str(Text,Str1) of
>    N when N /=0 -> do_a();
>    _ ->
>      case string:str(Text,Str2) of
>         N2 when N2 /= 0 -> do_b();
>         _ -> ....
>      end
> end
>
> OR:
>
> case string:str(Text,Str1) of
>    0 ->
>      case string:str(Text,Str2) of
>         0 -> do_c();
>         _ -> do_b();
>      end;
>    _ ->
>      do_a()
> end
>
> Are two obvious variants.
>
>> if (string:str(Text,String)  /= 0) -> ...;
>>    (string:str(Text,String2) /= 0) ->...
>> end
>>
>> but string:str is not accepted in a condition.
>>
>> I wrote a small function witch takes Text and a list of {Return_value,
>> String} which tries each string and return Return_value if it matches:
>>
>> find_strings(_, []) -> false;
>>
>> find_strings(Texte,[{Retour,Chaine}|Liste])  ->
>>         T1 = string:str(Texte, Chaine),
>>         case T1 of
>>                 0       -> find_strings(Texte,[Liste]);
>>                 _Else   -> Retour
>>         end
>>         .
>>
>> but I wanted to know if there was not an other elegant solution that
>> does not require a function.
>>
>> Regards.
>>
>> Raphaël.






More information about the erlang-questions mailing list