Write Term to File

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Write Term to File

obi458
How can i write term to file, for later using in file:consult?

I do:

R = io_lib:format("~tp.",[{"cn","漢"}]),
file:write_file(FileName,R)

But this gives:

{"cn",[28450]}.

I need:

{"cn","漢"}.

Any hints?

--
Grüße
Oliver Bollmann

Reply | Threaded
Open this post in threaded view
|

Re: Write Term to File

Michał Muskała

Those two values are exactly the same. Lists of integers representing codepoints and strings are two ways of representing the same value. Reading back with file:consult should reproduce exactly the same values.

 

$ erl

Erlang/OTP 23 [erts-11.1.5] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [hipe] [dtrace]

 

Eshell V11.1.5  (abort with ^G)

1> {"cn",[28450]} =:= {"cn",""}.

true

2> io:format("~tp.\n",[{"cn",""}]).

{"cn",[28450]}.

ok

 

If you’re asking if you can make io:format print the string representation rather than list one for high-codepoint characters, the answer is you can, by starting the VM in Unicode mode with +pc unicode flag:

 

$ erl +pc unicode

Erlang/OTP 23 [erts-11.1.5] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [hipe] [dtrace]

 

Eshell V11.1.5  (abort with ^G)

1> io:format("~tp.\n",[{"cn",""}]).

{"cn",""}.

ok

 

Michał.

 

From: erlang-questions <[hidden email]> on behalf of Oliver Bollmann <[hidden email]>
Date: Tuesday, 26 January 2021 at 22:52
To: Erlang-Questions Questions <[hidden email]>
Subject: Write Term to File

How can i write term to file, for later using in file:consult?

I do:

R = io_lib:format("~tp.",[{"cn",""}]),
file:write_file(FileName,R)

But this gives:

{"cn",[28450]}.

I need:

{"cn",""}.

Any hints?

--
Grüße
Oliver Bollmann

Reply | Threaded
Open this post in threaded view
|

Re: Write Term to File

obi458

That's true for file:consult, but

the user who want to edit the file will see

{"cn",[28450]}.

and not

{"cn",""}.

So, for the user it's different!

What is the right way to save?

Regards,

Oliver


On 27.01.21 00:01, Michał Muskała wrote:

Those two values are exactly the same. Lists of integers representing codepoints and strings are two ways of representing the same value. Reading back with file:consult should reproduce exactly the same values.

 

$ erl

Erlang/OTP 23 [erts-11.1.5] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [hipe] [dtrace]

 

Eshell V11.1.5  (abort with ^G)

1> {"cn",[28450]} =:= {"cn",""}.

true

2> io:format("~tp.\n",[{"cn",""}]).

{"cn",[28450]}.

ok

 

If you’re asking if you can make io:format print the string representation rather than list one for high-codepoint characters, the answer is you can, by starting the VM in Unicode mode with +pc unicode flag:

 

$ erl +pc unicode

Erlang/OTP 23 [erts-11.1.5] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [hipe] [dtrace]

 

Eshell V11.1.5  (abort with ^G)

1> io:format("~tp.\n",[{"cn",""}]).

{"cn",""}.

ok

 

Michał.

 

From: erlang-questions [hidden email] on behalf of Oliver Bollmann [hidden email]
Date: Tuesday, 26 January 2021 at 22:52
To: Erlang-Questions Questions [hidden email]
Subject: Write Term to File

How can i write term to file, for later using in file:consult?

I do:

R = io_lib:format("~tp.",[{"cn",""}]),
file:write_file(FileName,R)

But this gives:

{"cn",[28450]}.

I need:

{"cn",""}.

Any hints?

--
Grüße
Oliver Bollmann

-- 
Grüße
Oliver Bollmann
Reply | Threaded
Open this post in threaded view
|

Re: Write Term to File

zxq9-2
In reply to this post by obi458
Hi, Oliver.

The lack of an inverse function for file:consult/1 comes up from time to
time. I've written about it here:
https://zxq9.com/archives/1021

TL;DR: You want a function like this:


   -spec write_terms(Filename, Terms) -> ok
       when Filename :: file:filename(),
            Terms    :: [term()].
   %% @private
   %% Provides functionality roughly inverse to file:consult/1.

   write_terms(Filename, List) ->
       Format = fun(Term) -> io_lib:format("~tp.~n", [Term]) end,
       Text = unicode:characters_to_binary(lists:map(Format, List)),
       file:write_file(Filename, Text).


For the example I put this in a module called "writer".
The IMPORTANT thing to note is the flag passed to Erlang on startup:
`erl +pc unicode`

   ceverett@icecream:~/Code/erlang$ erl +pc unicode
   Erlang/OTP 23 [erts-11.1] [source] [64-bit] [smp:8:8] [ds:8:8:10]
[async-threads:1] [hipe]

   Eshell V11.1  (abort with ^G)
   1> c(writer).
   {ok,writer}
   2> writer:write_terms("blah.txt", ["Some English", "何かの日本語"]).
   ok
   3> q().
   ok
   4>
   ceverett@icecream:~/Code/erlang$ cat blah.txt
   "Some English".
   "何かの日本語".

Little details like this make the unicode world go round. I'm not quite
sure why Erlang doesn't default to unicode, but whatever.

Keep in mind that the function above expects to receive a list. That is,
you must called write_terms(Path, ["漢"]), not write_terms(Path, "漢").

-Craig

On 2021/01/27 7:52, Oliver Bollmann wrote:

> How can i write term to file, for later using in file:consult?
>
> I do:
>
> R = io_lib:format("~tp.",[{"cn","漢"}]),
> file:write_file(FileName,R)
>
> But this gives:
>
> {"cn",[28450]}.
>
> I need:
>
> {"cn","漢"}.
>
> Any hints?
>
Reply | Threaded
Open this post in threaded view
|

Re: Write Term to File

Michael P.
:-)

On Wed, 27 Jan 2021 12:39:52 +0900
zxq9 <[hidden email]> wrote:

> The lack of an inverse function for file:consult/1 comes up from time to

And I thought I was too stupid to find it or use the io(_lib) stuff.


> time. I've written about it here:
> https://zxq9.com/archives/1021

Will read it (sometime). I have archived this little gem of a post.

~M

--

Normality is merely a question of quantity,
not of quality.

Normalität ist nur eine Frage von Quantität,
nicht von Qualität.