AndyGates
AndyGates

Reputation: 171

Erlang Pattern Matching Problem

So i'm writing an Erlang program, and I have a message as a string coming in via a socket.

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

I have 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 just seems to crash if the message doesn't match.

I have a feeling I may be going about this completely the wrong way, but am not sure what else to try...

Cheers for any help or advice =]

EDIT: Ok nevermind! Replacing the "true->" with "_->" makes it work just fine -_- silly me!

I'd still be interested to know if this is the best way of going about this, or if there is a better way.

Cheers again :)

Upvotes: 4

Views: 1115

Answers (1)

keymone
keymone

Reputation: 8104

instead of true you have to use _ - wildcard which matches anything

P.S. oops, saw your edit too late.

answer to your second question would be - use functions instead of cases:

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

Upvotes: 3

Related Questions