Extraction of elements of tuples

32,241

Solution 1

Try this:

1> A = [{3,1,1444}].
[{3,1,1444}]
2> [{X, _, _}] = A.
[{3,1,1444}]
3> X.
3
4> 

Solution 2

Given that you get exactly what you state, a list with one tuple, even easier would be (using element/2)

element(1, hd(L)).

A pattern matching variant like shk suggested is probably even nicer, depending on the context.

Solution 3

you could also consider using records syntax if you want some semantics embedded into your tuples

-record(x, {y, z}).

1> A = #x{y=b, z=c}.
2> A#x.y.
b

all records are in fact tuples and you dont have to worry about order of elements in that tuple nor about adding/removing elements.

Share:
32,241
Bertaud
Author by

Bertaud

Updated on July 09, 2022

Comments

  • Bertaud
    Bertaud almost 2 years

    Given one list with one tuple:

    [{4,1,144}]
    

    How to extract the first element of the tuple inside the list:

    element(1,lists:nth(1,L))
    

    Do you have a simpler solution?