Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a flat string with integers in it in erlang?

In erlang, I want to format a string with integers in it and I want the result to be flattened. But I get this:

io_lib:format("sdfsdf ~B", [12312]).                 
[115,100,102,115,100,102,32,"12312"]

I can get the desired result by using the code below but it is really not elegant.

lists:flatten(io_lib:format("sdfsdf ~B", [12312])).
"sdfsdf 12312"

Is there a better formatting strings with integers in them, so that they are flat? Ideally, using only one function?

like image 615
Rebecca Meritz Avatar asked Jun 28 '11 07:06

Rebecca Meritz


People also ask

What does ## represent in a string?

'#' represents an optional digit place that will not appear in case of a 0 digit in that position, whereas 0 means the digit will always appear. Let's take 345.5 as an example: #,##0. ## = 345.5 0,000.00 = 0,345.50.

What string method is used to format values in a string?

The format() method formats the specified value(s) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}. Read more about the placeholders in the Placeholder section below. The format() method returns the formatted string.

What is string format () used for?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.


2 Answers

You flatten a list using lists:flatten/1 as you've done in your example.

If you can accept a binary, list_to_binary/1 is quite efficient:

1> list_to_binary(io_lib:format("sdfsdf ~B", [12312])).
<<"sdfsdf 12312">>

However, question why you need a flat list in the first place. If it is just cosmetics, you don't need it. io:format/1,2,3 and most other port functions (gen_tcp etc) accept so called deep IO lists (nested lists with characters and binaries):

2> io:format([115,100,102,115,100,102,32,"12312"]).
sdfsdf 12312ok
like image 79
Adam Lindberg Avatar answered Oct 18 '22 21:10

Adam Lindberg


There is an efficiency reason that io_lib:format returns deep lists. Basically it saves a call to lists:flatten.

Ask yourself why you want the list flattened. If you are going to print the list or send it to a port or write it to a file, all those operations handle deep lists.

If you really need a flattened list for some reason, then just flatten it. Or you can create your own my_io_lib:format that returns flattened lists if you think it important.

(If you only want to flatten the list for debugging reasons then either print your strings with ~s, or create a flattener in an erlang module named user_default. Something like this:

 -module(user_default).
 -compile(export_all).
 %% either this:
 fl(String) ->
    lists:flatten(String).
 %% or this:
 pp(String) ->
     io:format("~s~n", [String]).

Then you can use fl/1 and print/1 in the Erlang shell (as long as user_default.beam is in your path of course).)

like image 43
Daniel Luna Avatar answered Oct 18 '22 20:10

Daniel Luna