[erlang-questions] Ternary Like Operation Idiom in Erlang

Evans, Matthew <>
Wed Dec 2 22:56:14 CET 2009

Would this work?

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

For example:

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

-----Original Message-----
From:  [mailto:] 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".

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