Publication date: 07/08/2024

Locate Items in a List

Use Loc(), Contains(),or Where() to find values in a list:

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

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 in JSL Scripts.

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

Where( nameList == "Katie" );

[1]

Search for the value "Erin" in the nameList:

Loc( nameList, "Erin" );

[]

Contains( nameList, "Erin" );

0

Where( nameList == "Erin" );

[](0, 1)

Search for the number 8 in the numList:

Loc( numList, 8 );

[4, 5]

Contains( numList, 8 );

4

Where( numList == 8 );

[4, 5]

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).