Reputation: 21817
I have 2 list:
["asd", "dsa"].
[[123, "asd"], [4534, "fgh"]].
How can i generate next list: I ned list that tail of each nested list =:= other element of 1 list.
In this example:
["asd", "dsa"].
[[123, "asd"], [4534, "fgh"]].
"asd" =:= "asd" ->
Output list:
[123, "asd"]
I try:
Here S = [[123, "asd"], [4534, "fgh"]]. D = ["asd", "dsa"].
List = lists:filter(fun(X) -> lists:last(X) =:= D end, S),
But D in this example list, and i need element of list.
How can do it?
Upvotes: 3
Views: 267
Reputation: 20916
A slightly more direct way of writing it would be:
lists:filter(fun (X) -> lists:member(lists:last(X), D) end, S).
or with list comprehensions:
[ X || X <- S, lists:member(lists:last(X), D) ].
They are a little faster as they will not attempt to match against more elements in D
if the element is found. Expanding D
in the comprehension will do this.
Upvotes: 2
Reputation: 9676
Maybe something like:
1> [X || X<-[[1,2,4],[7,8,3],[2,5,4],[9,1,6]], Y<-[4,3], lists:last(X)=:=Y].
[[1,2,4],[7,8,3],[2,5,4]]
Or, using your sample data:
2> [X || X<-[[123,"asd"], [4534,"fgh"]], Y<-["asd","dsa"], lists:last(X)=:=Y].
[[123,"asd"]]
Upvotes: 3