Erlang pattern matching problem

So, I am writing an Erlang program, and I have a message in the form of a string coming in through a socket.

I need to check that the message is in the format: [Integer, Space, Integer, "\ r \ n"] for example "1 3 \ r \ n", and then only do something if the message matches this format.

I tried

case Move of [X1, 32 ,Y1,13,10]-> %do stuff.... true-> %don't do stuff... end 

It works fine if the message is correct, but it looks like it will work if the message does not match.

I have a feeling that I may not be all wrong about this, but I don’t know what else to try ...

Cheers for any help or advice =]

EDIT: Good, never mind! Replacing "true->" with "_->" makes it work just fine - stupid me!

I would still be interested to know if this is the best for this, or if there is a better way.

Greetings again :)

+4
source share
1 answer

instead of true you should use _ - a wildcard that matches anything

PS oops, you saw too late.

the answer to your second question will be: use functions instead of cases:

 f([X1, 32, Y1, 13, 10]) -> ...; f(_) -> ok. 
+3
source

Source: https://habr.com/ru/post/1337534/


All Articles