2

if I have a list of elements

FV:= [[ 1, 2, 4 ], [ 2, 4 ], [ 3, 1, 2, 4 ], [ 5, 3, 1, 2, 4 ], [ 15, 16, 18 ], 
     [ 16, 18 ], [ 17, 15, 16, 18 ],[ 19, 17, 15, 16, 18 ], [ 29, 30, 32 ], 
     [ 30, 32 ], [ 31, 29, 30, 32 ], [ 33, 31, 29, 30, 32 ], [ 43, 44, 46 ],
     [ 44, 46 ], [ 45, 43, 44, 46 ], [ 47, 45, 43, 44, 46 ], [ 57, 58, 60 ],
     [ 58, 60 ], [ 59, 57, 58, 60 ],[ 61, 59, 57, 58, 60 ], [ 74, 75, 76, 77 ], 
     [ 75, 76, 77 ], [ 76, 77 ], [ 81, 82, 83, 84 ], [ 82, 83, 84 ],[ 83, 84 ], 
     [ 88, 89, 90, 91 ], [ 89, 90, 91 ], [ 90, 91 ], [ 95, 96, 97, 98 ],
     [ 96, 97, 98 ], [ 97, 98 ],[ 102, 103, 104, 105 ], [ 103, 104, 105 ], 
     [ 104, 105 ]]

and I want to select only those elements that are of length $2$ in this list, I am trying to use Filtered function like this

gap> Filtered(FV,n->Length(FV[n]=2));

but it doesn't work. How should I use this function operation to find the right answer as I want.

Mikasa
  • 67,374
S786
  • 1,001
  • 2
    The argument of the function used in Filtered is not the index, but the element itself. It should be: Filtered(FX, u -> Length(u)=2);. I have not GAP at hand to check right now, but it should work. – Jean-Claude Arbaut Aug 22 '14 at 19:09
  • Yes it worked @Jean Thanks. – S786 Aug 22 '14 at 19:56
  • 1
    Also, Filtered(FV,n->Length(FV[n]=2)) is not going to work because of the misplaced bracket: instead of Length(FV[n]=2) one should use Length(FV[n])=2. Otherwise FV[n]=2 results in true or false and then Length can not be applied to a boolean, so you will have a "no method found" error. – Olexandr Konovalov Aug 23 '14 at 19:45

2 Answers2

3

This command can work for it.

gap> Filtered(FV, u -> Length(u)=2);
Mikasa
  • 67,374
S786
  • 1,001
  • 2
    Yes, and if you're interested in positions in the list, rather then in its actual elements, then use Filtered([1..Length(FV)],n->Length(FV[n])=2); – Olexandr Konovalov Aug 23 '14 at 19:43
2

However, the posted code can solve the problem swiftly; it might be done by another basic way as follows:

gap> e:=Elements(FV);;
     for i in [1..Size(FV)] do if Length(e[i])=2 then Print(e[i], "\n"); fi; od;

[ 2, 4 ]
[ 16, 18 ]
[ 30, 32 ]
[ 44, 46 ]
[ 58, 60 ]
[ 76, 77 ]
[ 83, 84 ]
[ 90, 91 ]
[ 97, 98 ]
[ 104, 105 ]
Olexandr Konovalov
  • 7,002
  • 2
  • 34
  • 72
Mikasa
  • 67,374