0

I'm trying to use "_" in Case but I'm missing some thing. What i'm doing is :

case (Packet =:= #xmlel{name = <<"message">>, attrs = [_, {<<"type">>,<<"chat">>}], children = _}) of
    true ->
        ?INFO_MSG("True ###### Packet ~p", [Packet]);
    _ ->
        ?INFO_MSG("False ###### Packet ~p", [Packet])
end,

And the error is : variable '_' is unbound.

I want this variable "_" to mean in this function every thing.

Like -->

attrs = [Whatever, {<<"type">>,<<"chat">>}]

children = Whatever

How can I do it? thnx.

Mr. zero
  • 245
  • 4
  • 18

1 Answers1

2

The problem is:

You cannot use '_' on the right of '='

You can only put it on the left of the '='

e.g.

{_,4} = {x,y} (correct)

{x,y} = {_,4} (wrong)

Xin
  • 33,823
  • 14
  • 84
  • 85
  • Ok, is there any thing mean "Whatever" can I use it ? Like : Packet = {xmlel, <<"message">>, [{<<"to">>,<<"ttt@ttt.ttt">>},{<<"type">>,<<"chat">>}], []} I want something mean everything in Erlang Like Packet == #xmlel{name = <<"message">>, attrs = [WhatEever, {<<"type">>,<<"chat">>}], children = WhatEever}) – Mr. zero Mar 02 '16 at 03:06
  • I do not fully understand that. '_' means match everything. _=1 and _=2 will always pass. However a = 1 and a = 2 will crash. – Xin Mar 02 '16 at 03:52
  • I think I got what u mean. Basically, this is not the correct way to go, and Erlang does not support that for you. Pattern=Expression will check and match for you, you cannot put unknown stuff on the right of =. This is not the design of Erlang – Xin Mar 02 '16 at 03:57
  • thank you. Can I know if some text count something like : te = "hi how are you today." if te == "hi" -> – Mr. zero Mar 02 '16 at 04:07
  • case Expression of Pattern1 -> XXOO; Pattern2 -> OOXX; _ -> XXXX end, if X<1 -> XX; X>10 -> OO; true -> XXOO end – Xin Mar 02 '16 at 04:14