Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to read TXT file content and parse it into Erlang tuples?

I've got a TXT file in the form of tuples:

{187,386,858}.
{46,347,621}.
{28,856,354}.
{246,298,574}.
{979,964,424}.
{664,722,318}.
{82,69,64}.

I need to read the file, so i get a list of usable tuples to pass {Key, Num1, Num2} to a function.

I have tried using binary:split, but that doesn't get me quite there:

{ok, Device} = file:read_file(Filename),
    Tuples = [binary_to_list(Bin) || Bin <- binary:split(Device,<<".\n">>,[global])].

I will get a result like this:

["{187,386,858}","{46,347,621}","{28,856,354}",
 "{246,298,574}","{979,964,424}","{664,722,318}",
 "{82,69,64}","{654,351,856}","{185,101,57}","{93,747,166}",
 "{41,442,946}","{444,336,300}","{951,589,376}",
 "{193,987,300}",[]]

but looking to get something more in this format:

[{979, 193, 224}, {365, 339, 950}, {197, 586, 308},
 {243, 563, 245}, {795, 534, 331}, {227, 736, 701},
 {111, 901, 185}, {303, 178, 461}, {361, 212, 985},
 {149, 659, 496}, {612, 375, 311}, {896, 402, 10}]
like image 317
Jose Arnaldo Araya Castrillo Avatar asked Dec 29 '25 12:12

Jose Arnaldo Araya Castrillo


1 Answers

You have file:consult/1 that parses Erlang terms directly from a file.

like image 131
José M Avatar answered Jan 01 '26 06:01

José M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!