views:

46

answers:

1

Ok now I think Im getting warmer, I have to pattern match whatever comes in.

So if I had say

Message = = [[<<>>], 
 [<<"10">>,<<"171">>],
 [<<"112">>,<<"Gen20267">>],
 [<<"52">>,<<"20100812-06:32:30.687">>]] 

And I was looking to pattern match the field <<"112">>

Such as the 112 is always going to say 112, but the Gen2067 can change whenever to whatever.. its the data, it will be stored in a variable.

Also the fields can be in any order, whatever function im trying to do has to be able to find the field and parse it.

This is the code I am using right now:

loop() ->
receive
    [_,[<<"112">>, Data], _] when is_list(X) -> %% Just dosen't work in anyway..
        ?DEBUG("Got a list ~p~n", [X]),
        loop();
    _Other ->
        ?DEBUG("I don't understand ~p~n", [_Other]),
        loop()
end.

I feel im close, but not 100%

-B

A: 

You can extract your data this way:

1> Message = [[<<>>],                        
1>  [<<"10">>,<<"171">>],                    
1>  [<<"112">>,<<"Gen20267">>],              
1>  [<<"52">>,<<"20100812-06:32:30.687">>]] .
[[<<>>],
 [<<"10">>,<<"171">>],
 [<<"112">>,<<"Gen20267">>],
 [<<"52">>,<<"20100812-06:32:30.687">>]]
2> [Data] = [X || [<<"112">>, X] <- Message ].
[<<"Gen20267">>]
3> Data.
<<"Gen20267">>

Another way:

4> [_, Data] = hd(lists:dropwhile(fun([<<"112">>|_]) -> false; (_)->true end, Message)).
[<<"112">>,<<"Gen20267">>]
5> Data.
<<"Gen20267">>

And another one as function in module (probably fastest):

% take_data(Message) -> Data | not_found
take_data([]) -> not_found;
take_data([[<<"112">>, Data]|_]) -> Data;
take_data([_|T]) -> take_data(T).
Hynek -Pichi- Vychodil