Scripting Guide > Data Structures > Lists > Locate Items in a List
Publication date: 08/13/2020

Locate Items in a List

Use the Loc() function or the Contains() function to find values in a list:

Loc( list, value );
Contains( list, value );

Loc() and Contains() return the positions of the values. Loc() returns the results in a matrix, and Contains() returns the results as a number.

Notes:

The Loc function returns each occurrence of a repeated value. Contains() returns only the first occurrence of a repeated value.

If the value is not found, the Loc function returns an empty matrix and Contains() returns a zero.

To assess whether an item is in a list, use Loc() and Contains() with >0. A returned value of zero means that the item is not in the list. A returned value of 1 means that the item is in the list at least once.

Note: For more information about matrix manipulation and a description of the equivalent Loc() command for matrices, see Matrices.

Examples

nameList = {"Katie", "Louise", "Jane", "Jane"};
numList = {2, 4, 6, 8, 8};

Search for the value "Katie" in the nameList:

Loc( nameList, "Katie" );

[1]

Contains( nameList, "Katie" );

1

Search for the value "Erin" in the nameList:

Loc( nameList, "Erin" );

[]

Contains( nameList, "Erin" );

0

Search for the number 8 in the numList:

Loc( numList, 8 );

[4, 5]

Contains( numList, 8 );

4

Find out if the number 5 exists in the numList:

NRow( Loc( numList, 5 ) ) > 0;

0

Contains( numList, 5 ) > 0;

0

Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).
.