your mail

Hugo Mills hugo@REDACTED
Fri Jul 31 21:26:47 CEST 2020


On Fri, Jul 31, 2020 at 10:11:05PM +0300, Papa Tana wrote:
> Hi,
> 
> I receive Msg from some device in the network, and Msg exist only in 2
> versions, like below :
> 
> Version1:
> -------------
> Msg = [{outnum,"configure"}].
> 
> OR
> 
> Version2:
> -------------
> Msg =
> [{outnum,"configure"},
>  {innum, challenge,"Rand","Kc"},
>  {innum, response ,"Rand","Sres"}].
> 
> I tried to make a list comprehension but the structure of each item is
> different depending it's a outnum(2) or an innum(4).
> 
> I need to process the Msg list, so that:
> 
>  - if Msg is type Version 1, ie contains outnum ONLY, do action for outnum;
>  - if Msg contains BOTH innum + outnum (Version2), do action inoutnum;
> 
> Can anyone advice please?

Guard expressions:

process_message(Msg) when length(Msg) =:= 1 ->
    outnum(Msg);
process_message(Msg) ->
    inoutnum(Msg).

or pattern matching:

process_message([{outnum, _}]) ->
    outnum(Msg);
process_message(Msg) ->
    inoutnum(Msg).

   Hugo.

-- 
Hugo Mills             | Is a diversity twice as good as a university?
hugo@REDACTED carfax.org.uk |
http://carfax.org.uk/  |
PGP: E2AB1DE4          |                                         Peter McGowan


More information about the erlang-questions mailing list