small erlang question.
Vladimir Sekissov
svg@REDACTED
Fri Jan 16 00:05:38 CET 2004
Good day,
Yet another variant without functions:
Result =
catch
begin
(0 == string:str(Text,Str1)) andalso (throw(Ret1)),
(0 == string:str(Text,Str2)) andalso (throw(Ret2)),
(0 == string:str(Text,Str3)) andalso (throw(Ret3)),
none
end,
...
or if you can change string:str/2 to return true|false
Result =
catch
begin
string:str(Text,Str1) andalso (throw(Ret1)),
string:str(Text,Str2) andalso (throw(Ret2)),
string:str(Text,Str3) andalso (throw(Ret3)),
none
end,
...
Best Regards,
Vladimir Sekissov
peter> But better is:
peter>
peter> case my_str(Text,[Str1,Str2,Str3]) of
peter> 1 -> do_1();
peter> 2 -> do_2();
peter> 3 -> do_3();
peter> none -> ok
peter> end
peter>
peter> where:
peter>
peter> my_str(Text,L) -> my_str(Text,L,1).
peter> my_str(Text,[],N) -> none;
peter> my_str(Text,[Str|T],N) ->
peter> case string:str(Text,Str) of
peter> 0 -> my_str(Text,T,N+1);
peter> _ -> N
peter> end.
peter>
peter> But that was maybe about what you did yourself...
peter>
peter> > case string:str(Text,Str1) of
peter> > N when N /=0 -> do_a();
peter> > _ ->
peter> > case string:str(Text,Str2) of
peter> > N2 when N2 /= 0 -> do_b();
peter> > _ -> ....
peter> > end
peter> > end
peter> >
peter> > OR:
peter> >
peter> > case string:str(Text,Str1) of
peter> > 0 ->
peter> > case string:str(Text,Str2) of
peter> > 0 -> do_c();
peter> > _ -> do_b();
peter> > end;
peter> > _ ->
peter> > do_a()
peter> > end
peter> >
peter> > Are two obvious variants.
peter> >
peter> >> if (string:str(Text,String) /= 0) -> ...;
peter> >> (string:str(Text,String2) /= 0) ->...
peter> >> end
peter> >>
peter> >> but string:str is not accepted in a condition.
peter> >>
peter> >> I wrote a small function witch takes Text and a list of {Return_value,
peter> >> String} which tries each string and return Return_value if it matches:
peter> >>
peter> >> find_strings(_, []) -> false;
peter> >>
peter> >> find_strings(Texte,[{Retour,Chaine}|Liste]) ->
peter> >> T1 = string:str(Texte, Chaine),
peter> >> case T1 of
peter> >> 0 -> find_strings(Texte,[Liste]);
peter> >> _Else -> Retour
peter> >> end
peter> >> .
peter> >>
peter> >> but I wanted to know if there was not an other elegant solution that
peter> >> does not require a function.
peter> >>
peter> >> Regards.
peter> >>
peter> >> Raphaël.
peter>
peter>
peter>
More information about the erlang-questions
mailing list