No subject

Leonard B leonard.boyce@REDACTED
Fri Jul 31 21:36:51 CEST 2020


You could just head match of that specification is reliable and has not variance

msg_type([{outnum, _}]) -> 1; %% only has outnum
msg_type(_) -> 2. %% may have other values

or if unreliable:

msg_type([{outnum, _}]) -> 1; %% only has outnum
msg_type(L) ->
  %% Only contains innum and outnum, badmatch crash if not
  [innum, outnum] = lists:usort([element(1,Kv) || Kv <- L]),
  2.

```
1> MT = fun MT([{outnum, _}]) -> 1; MT(L) -> [innum, outnum] =
lists:usort([element(1,KV) || KV <- L]), 2 end.
#Fun<erl_eval.30.50752066>
2> Spec1 = [{outnum,"configure"}].
[{outnum,"configure"}]
3> Spec2 = [{outnum,"configure"},{innum,
challenge,"Rand","Kc"},{innum, response ,"Rand","Sres"}].
[{outnum,"configure"},
{innum,challenge,"Rand","Kc"},
{innum,response,"Rand","Sres"}]
4> MT(Spec1).
1
5> MT(Spec2).
2
6> MT([{other,sdsf} | Spec2]).
** exception error: no match of right hand side value [innum,other,outnum]
7>
```


Kind regards,
Leonard


On Fri, Jul 31, 2020 at 3:11 PM Papa Tana <papa.tana101@REDACTED> 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?
>
> Thanks,


More information about the erlang-questions mailing list