2

I have a problem with pattern match of json formatted string. Here I add a shorted version (just changed long json string to "{\"jsondata\"}" So i have this pattern match which is sucessfull:

 > MyData2={ok,{{"HTTP/1.1",200,"OK"},
         [{"connection","Keep-Alive"},
          {"date","Thu, 10 Sep 2015 12:03:49 GMT"},
          {"server","Apache/2.4.7 (Ubuntu)"},
          {"vary","X-Auth-Token"},
          {"content-length","1171"},
          {"content-type","application/json"},
          {"x-openstack-request-id",
           "req-31b4efc1-2af4-4130-b7a8-01d94b456096"},
          {"keep-alive","timeout=5, max=100"}],
         "{\"jsondata\"}"}}.

After that I run the following:

> {ok,{{"HTTP/1.1",ReturnCode, State},B,J}}=MyData2.

unfortunatelly i get If I change "{\"jsondata\"}" to "jsondata" the last pattern match works fine I have no Idea how to extract the json and get in J the "{\"jsondata\"}" I`ll appriciate any idea ** exception error: no match of right hand side value

YAKOVM
  • 9,805
  • 31
  • 116
  • 217
  • 1
    It seems like you're getting this error because `MyData2` is already bound in the shell. See [this answer](http://stackoverflow.com/a/22281376/113848) for more information about that problem. – legoscia Sep 10 '15 at 14:43
  • @legoscia - no it`s a new one and is not bound – YAKOVM Sep 10 '15 at 14:44
  • Is it possible, that the variables `ReturnCode`, `State`, `B` or `J` are already bound? That would lead to a similiar problem, because they will be replaced by their bound values on the left hand side. – Mathias Vonende Sep 10 '15 at 14:54
  • 1
    If I copy-paste everything and execute it in a new shell, the matching works perfectly. It has to be a variable-binding issue, like I just posted... – Mathias Vonende Sep 10 '15 at 15:01
  • 2
    Same results as @maze-le: copy & paste everything in a new shell and it works fine. Also, just think about it: the only way to get an error message "no match of right hand side value" is for something on the left-hand side to already have a value that doesn't match the right-hand side... – Steve Vinoski Sep 10 '15 at 17:16

1 Answers1

3

Your pattern matching operation works perfectly. I think the problem is, that one of the variables ReturnCode, State, B or J is already bound.

Lets assume the variable J is already bound to a value, and the other variables are not. Depending on this value, the pattern matching operation

{ok,{{"HTTP/1.1",ReturnCode, State},B,J}} = MyData2.

either succeeds or not.

Case 1:J is already bound to "{\"jsondata\"}"

Your pattern-match will succeed and the values of the unbound variables (ReturnCode, State and B) will be set, according to the pattern of MyData2.

Case 2:J is already bound to "{jsondata}"

The J-variable on the right hand side won't match the pattern of MyData2 on the left hand side. Thus the execution fails with an exception.

This also happens on the shell if you forget to clear your variables with f(Variable).

Mathias Vonende
  • 1,400
  • 1
  • 18
  • 28