[erlang-questions] Ternary Like Operation Idiom in Erlang

Evans, Matthew mevans@REDACTED
Wed Dec 2 22:56:14 CET 2009


Would this work?

hd(re:split(Value,"=",[{return,list}])) ++ "=true".

For example:

(scm@REDACTED)1> Test1 = "key".
"key"
(scm@REDACTED)2> Test2 = "key=true".
"key=true"
(scm@REDACTED)3>
(scm@REDACTED)3> hd(re:split(Test1,"=",[{return,list}])) ++ "=true".
"key=true"
(scm@REDACTED)4> hd(re:split(Test2,"=",[{return,list}])) ++ "=true".
"key=true"

-----Original Message-----
From: erlang-questions@REDACTED [mailto:erlang-questions@REDACTED] On Behalf Of Jarrod Roberson
Sent: Wednesday, December 02, 2009 4:13 PM
To: Erlang
Subject: [erlang-questions] Ternary Like Operation Idiom in Erlang

In multicast dns TXT records you have name value pairs that can look
like "key=value", "key=" and "key".
"key=" means key with no values
"key" implies "key=true"
I need to convert any thing that looks like "key" to "key=true" to
normalize my data I am converting to dictionaries using re:split/2.

here is how I would do this in Java.

String s = "xxx";
return s.matches("=") ? s : s + "=true";

here is what I have come up with in Erlang.

fix(T,{match,_}) -> T;
fix(T,nomatch) -> string:concat(T,"=true").

where I call it like
S = "key".
fix(S,re:run(S,"=")).

Ignoring that the function name "fix" needs to be more descriptive.
Is this the best way to do something like this? Or is there some fancy
one line way of doing this?

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org



More information about the erlang-questions mailing list