Erlang/OTP Forums

Author Message

<  Erlang  ~  record question, (Likely being a noob).

bapril
Posted: Wed Apr 15, 2009 5:32 pm Reply with quote
Joined: 15 Apr 2009 Posts: 1
I am working on an application where I have a large stream of records going by. In the application there are multiple record-types, but I can explain my problem with only one.

Code:
-record(test, {a,b,c}).


I would like to be able to pass in erlang syntax expressions for filtering after compile-time. It works just fine if I hard-code the expression e.g.

Code:
9> A = #test{a=1,b=2,c=3}.
#test{a = 1,b = 2,c = 3}
10> #test{a=1} = A.
#test{a = 1,b = 2,c = 3}
11> #test{a=3} = A.
** exception error: no match of right hand side value #test{a = 1,b = 2,c = 3}


The problem is that I have to know both what type of record and the structure of the filter I want to create at compile-time (which I do not). I would like to be able to do the following:

Code:
12> B = #test{a=1}.
#test{a = 1,b = undefined,c = undefined}
13> B = A.
** exception error: no match of right hand side value #test{a = 1,b = 2,c = 3}


Is there a way to convert a variable back into an expression?

Thanks
Ben
View user's profile Send private message
uwiger
Posted: Thu Apr 16, 2009 10:42 am Reply with quote
User Joined: 03 Jul 2006 Posts: 604 Location: Sweden
bapril wrote:

The problem is that I have to know both what type of record and the structure of the filter I want to create at compile-time (which I do not). I would like to be able to do the following:

Code:
12> B = #test{a=1}.
#test{a = 1,b = undefined,c = undefined}
13> B = A.
** exception error: no match of right hand side value #test{a = 1,b = 2,c = 3}


Is there a way to convert a variable back into an expression?


Well, your code could be written:

Code:
14> Matches = fun(#test{a = 1}) -> true;
                       (_) -> false
                    end.
15> Matches(A).
true


Or you could use some form of key-value lists instead of records. Look e.g. at orddict.

BR,
Ulf W
View user's profile Send private message Visit poster's website
rvirding
Posted: Sun Apr 19, 2009 8:54 pm Reply with quote
User Joined: 30 Aug 2006 Posts: 452 Location: Stockholm, Sweden
Records are compile time preprocessor to tuples giving named elements, there is no record data type.
View user's profile Send private message Visit poster's website MSN Messenger

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum