irf_zack
irf_zack

Reputation: 33

lists:map() on mnesia:table_info(table, attributes)

I want to do operation on Mnesia table on each attribute of the table.

Record = #table{attr1, attr2, attr3, attr4},

mnesia:table_info(Table, attributes) gives a list of attributes which are atoms i.e [attr1, attr2, attr3, attr4]

lists:map(fun(X) -> Record#table.X end, mnesia:table_info(Table, attributes))

I'm expecting the above function to give list of values. But, i'm getting error

"  * 1: syntax error before: X  "

I have already defined the table structure using the shell command -rd(table, {}) and also have used -rr(module) to read the record structure

What is wrong in the above code?? Any, alternative to process the table attributes using lists module??

Upvotes: 1

Views: 210

Answers (1)

Lol4t0
Lol4t0

Reputation: 12547

You cannot refer to record items by name in runtime. It's one of record drawbacks.

In runtime records are just tuples with the first element being the name of record, so

#table{attr1 = 1, attr2 = 2, attr3 = 3, attr4 = 4} =:= {table, 1, 2, 3, 4}

As you can see, all naming information is lost.

But all your function looks a bit useless. If you want to get all values, you can use record itself.

Upvotes: 1

Related Questions