Forcing a subscripted list always to be a list?<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />Here's a code snippet:
a={"A","B","C","D",123};
sub1 = list(1,3,5);
sub2 = list(1);
show(a[sub1]);
show(a[sub2]);
Output:
a[sub1] = {"A", "C", 123};
a[sub2] = "A";
Is there some way of subscripting a list with an arbitrary number of elements, and always have the output be a list?
Re: Forcing a subscripted list always to be a list?Hi Martin,
This is something we are "fixing" for JMP 13. I put "fixing" in quotes because it's possible some legacy scripts are dependent on the old behavior and will consider the fix to be a break. If that proves to be a big issue, we may have to find an alternative.
It's good to see your message which supports the idea of a fix.
Re: Forcing a subscripted list always to be a list?Here's a better one that works on lists of lists.
a={{"A"},{"B"},{"C"},{"D"},{123}};
sub1 = list(1,3,5);
sub2 = list(1);
ForceList = Function( {forceme},
If(Is List(forceme), forceme, EvalList(List(forceme)))
);
SubscriptList = Function( {inlist, subscriptlist},
if(N items(subscriptlist) == 1,
EvalList(List(inlist[subscriptlist]))
,
inlist[subscriptlist]
)
);
Output:
show( ForceList(a[sub1]));
show( ForceList(a[sub2]));
{{"A"}, {"C"}, {123}};
{"A"};
show( SubscriptList(a,sub1));
show( SubscriptList(a,sub2));
{{"A"}, {"C"}, {123}};
{{"A"}};
Re: Forcing a subscripted list always to be a list?Yeah, that's kind of what I figured. I wrote a simple "helper function" which I think I can use on subscripted lists. Thanks!
ForceList = Function( {forceme},
If(Is List(forceme), forceme, EvalList(List(forceme)))
);
show( ForceList(a[sub1]));
show( ForceList(a[sub2]));
Output:
{"A", "C", 123};
{"A"};
Re: Forcing a subscripted list always to be a list?When your subscript only has one value, it is always going to return the single value. If your subscript is a list with multiple values, it is going to return all the values in the form of a list.
To get around this you need an If statement to check for how many values are in your subscript list and wrap the return if the subscript has only one value.
Here is an example I put together:
a = {"A", "B", "C", "D", 123};
sub1 = List( 1, 3, 5 );
sub2 = List( 1 );
Eval( Eval Expr( indicesList = {Expr( sub1 ), Expr( sub2 )} ) );
For( i = 1, i <= N Items( indicesList ), i++,
If( N Items( indicesList[i] ) > 1,
temp = a[indicesList[i]],
temp = Eval( Eval Expr( List( Expr( a[indicesList[i]] ) ) ) )
);
Show( temp );
);