tags:

views:

88

answers:

3

How do I write a pattern that will bind a variable to the second element in this tuple {<0.206.0>, {rect, 10, 30}}?

I.e. "thing in place of pattern here" that results in Shape having the value {rect, 10, 30}. Pattern = {<0.206.0>, {rect, 10, 30}}

It's the <0.206.0> part that is confusing me.

+8  A: 

As simple as this...

{_, Shape} = {<0.206.0>, {rect, 10, 30}}.

However, you cannot create a process-id from a literal, so the above syntax is wrong, but try this in the REPL...

1> {_, Shape} = {self(), {rect, 10, 30}}.
{<0.31.0>,{rect,10,30}}
2> Shape.
{rect,10,30}
dsmith
Yeah, thanks. I was just about to post that self() worked, even if it gives different numbers. :)The question is pretty weird though, since the lecturer hasn't talked about PIDs at all. Oh well, this should do.
Peterx
+4  A: 

<0.206.0> looks like a PID. It's just one thing either way, so don't think about it.

You can do what dsmith suggests, depending on your context, or you can just do this:

Shape = element(2, {<0.206.0>, {rect, 10, 30}}).
Dustin
+3  A: 

Just for your further reference, you can use the pid/0 function to "create" a specific Pid:

1> Pid = pid(0,206,0).
<0.206.0>
2> {Pid, Shape} = {Pid, {rect, 10, 30}}.
{<0.206.0>,{rect,10,30}}
Roberto Aloi