Publication date: 07/08/2024

Iterate through a List

Iterate through a list to do something with each value or look for a particular value. The following JSL script looks at each item in the list. If the item in the list is less than or equal to 10, it is replaced with its square.

x = {2, 12, 8, 5, 18, 25};
n = N Items( x );
For( i = 1, i <= n, i++,
	If( x[i] <= 10,
		x[i] = x[i] ^ 2
	)
);
Show( x );

x = {4, 12, 64, 25, 18, 25};

You can use Loc() to locate the items in the new list that are equal to 25:

Loc( x, 25 );

[4, 6] // The fourth and sixth items in the list are equal to 25.

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