[erlang-questions] Help

26 views
Skip to first unread message

Lucky Khoza

unread,
Nov 13, 2012, 4:15:09 AM11/13/12
to erlang-q...@erlang.org
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"]]}.

As i said i'm newbie in Erlang Functional Programming i would really appreciate any help.


Kindest Regards
Lucky

Joseph Wayne Norton

unread,
Nov 13, 2012, 4:41:48 AM11/13/12
to Lucky Khoza, erlang-q...@erlang.org

Lucky -

Try reading one or more of these resources.

- http://learnyousomeerlang.com/ets
- http://en.citizendium.org/wiki/Erlang_(programming_language)/Tutorials/ETS
- http://en.wikibooks.org/wiki/Erlang_Programming/Using_ets

I found the above links by searching for "erlang and ets and tutorial".

Good luck!
> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Richard O'Keefe

unread,
Nov 13, 2012, 4:14:52 PM11/13/12
to Lucky Khoza, erlang-q...@erlang.org

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.
Reply all
Reply to author
Forward
0 new messages