[erlang-questions] Help

Richard O'Keefe ok@REDACTED
Tue Nov 13 22:14:52 CET 2012


On 13/11/2012, at 10:15 PM, Lucky Khoza wrote:

> Good day Erlang Developers,
> 
> I'm actually having a tuple like the one below: my question is  - How do i create a function that will store this tuple in a record that i will use create tag table in ETS.
> 
> {ok,[["\"Number of Units\"","\"Unit Price\"", "\"Delivery Date\"","\"Product Description\"", "\"Product Code\"","\"Supplier ID\""],
>      ["5","1505","\"2012/02/15\""," Mrs. Hollingberry\"","\"Apples 1kg Golden Delicious. The sweetest Apples! Always a favourite. Love", "1101","15"],
>      ["2","1423","\"2012/02/16\""," Mrs. Hollingberry\""," shame... Love", "\"Apples 1kg Green. They are very crunchy!", "1102","15"]]}.


This is some of the Hollingberries data, reversed.
And I have to tell you that it makes NO sense to use ETS
anywhere in solving the Hollingberries problem.

For each line of produce.csv, you are to produce one or more lines
of output.  NO information is carried from one line to the next.
Having put a record into an ETS table, you would never get it out again.

Now to answer your question as stated.

You would NOT put that tuple in an ETS table.
It has the form {ok,[L0,L1,...,Ln]}
where L0 is a header line and L1...Ln are data lines.

You would convert _each data line_ to a record and put _them_
into a table.

Step 1 is to define a record.
-record(what_you_want_to_call_the_record, {
    your_first_field_name,
    ...,
    your_last_field_name
  }).

Step 2 is to write a function that converts one line to
a record.  The order of the fields in the file is fixed.
convert([Number,Price,Delivery,Description,Code,Supplier]) ->
    #what_you_want_to_call_the_record{
	your_first_field_name = What_You_Want_To_Put_First,
	...,
	your_last_field_name = What_You_Want_To_Put_Last
    }.

Step 3 is to convert the data lines in your tuple to a list
of records.

    {ok,[_Header|Lines]} = The_Tuple_You_Showed_Us,
    lists:map(fun convert/1, Lines)

This gives you a list of records which you can do what you please
with.




More information about the erlang-questions mailing list