Erlang string for atom and string formatting

Using only list_to_atom() , you will get:

 list_to_atom("hello"). hello list_to_atom("Hello"). 'Hello' 

why is the difference?

I am trying to format a string with numbers, strings and atoms as follows:

 lists:flatten(io_lib:format("PUTVALUE ~p ~p", [list_to_atom("hello"), 40])). "PUTVALUE hello 40" lists:flatten(io_lib:format("PUTVALUE ~p ~p", [list_to_atom("Hello"), 40])). "PUTVALUE 'Hello' 40" 

What is the best way to do this in Erlang?

Edit: To clear the question, there are more values ​​than the example above, and in some cases this value can be a string or an atom, e.g.

 lists:flatten(io_lib:format("PUTVALUE ~p ~p ~p", [list_to_atom("hello"), X, 40])). 

where the first parameter is always a string, but X can be an atom or a string. The third parameter is always a number.

+4
source share
3 answers

If you want a flat list for strings and integers, the use of ~s and ~B may be limited:

 lists:flatten(io_lib:format("PUTVALUE ~s ~B", ["Hello", 40])). 
+9
source

In Erlang, an atom begins with a lowercase letter. In order for an atom to start with an uppercase letter, it must be enclosed in single quotes.

http://www.erlang.org/doc/reference_manual/data_types.html#id66663

+8
source

You can use: concat lists to format such a string

  lists:concat(["PUTVALUE ",hello," ",40]). 
+5
source

Source: https://habr.com/ru/post/1396960/


All Articles