Subscribe Bookmark RSS Feed

How to find locations in the list that satisfy some condition?

miguello

Community Trekker

Joined:

Jan 27, 2016

There is a way to find locations in the list for specific item.

For instance:

 

Loc([1 0 1 0 1 0]) = [1, 3, 5];
Loc({"A", 2, 3, 2, 5, 2, 4, [1 5]}, 2) = [2, 4, 6];
Loc({"A", 2, 3, 2, 5, 2, 4, [1 5]}, [1 5]) = [8];

Similarly to that, I want to find all locations in the list, where values are less (or greater) than a certan number.

 

Something alont the lines:

myList={3,5,2,6,9,13,17};
Loc(myList<6) = [1,2,3];

Is there a simple way of doing that?

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson

Super User

Joined:

Jun 22, 2012

Solution

As far as I know, there isn't such a function, however, here is a function that I built that does what you want:

a = {1, 3, 3, 2, 4, 2};

myLoc = Function( {list, x, comparison},

	result = [];
	For( i = 1, i <= N Items( list ), i++,
		Eval(
			Parse(
				"If(list[i] " || Eval( comparison ) || " " || Char( Eval( x ) ) || ",result = result |/ i);"
			)
		)
	);
	result;
);

test = myloc( a, 2, ">" );
Jim
1 REPLY
txnelson

Super User

Joined:

Jun 22, 2012

Solution

As far as I know, there isn't such a function, however, here is a function that I built that does what you want:

a = {1, 3, 3, 2, 4, 2};

myLoc = Function( {list, x, comparison},

	result = [];
	For( i = 1, i <= N Items( list ), i++,
		Eval(
			Parse(
				"If(list[i] " || Eval( comparison ) || " " || Char( Eval( x ) ) || ",result = result |/ i);"
			)
		)
	);
	result;
);

test = myloc( a, 2, ">" );
Jim