Choose, Lookup, and finding offset values
I’m wanting to find the n’th value in a list (specifically a vector in a matrix).
The obvious function would appear to be Choose – but this seems to require an explicit comma delimited list following the offset i.e. per the Sample:
[code:3c1nm1n6]result = choose(3,Lists::B1,Lists::B2,Lists::B3,Lists::B4,Lists::B5,Lists::B6,Lists::B7,Lists::B8,Lists::B9,Lists::B10)[/code:3c1nm1n6]
whereas this (which is what I need, since I don’t know in advance how many items may be in the category) fails:
[code:3c1nm1n6]bad = choose(3,Lists::B)[/code:3c1nm1n6]
On the other hand with Lookup I can have
[code:3c1nm1n6]result = lookup(3,Key List::B,Lists::B) [/code:3c1nm1n6]
(I don’t need to state the range explicitly as in the sample:
[code:3c1nm1n6]result = lookup(<foo>,Lists::B1 .. B10,Key List::B1 .. B10) [/code:3c1nm1n6])
But that requires me to create a dummy Key List with 1 – n as values for Items named 1 – n. Plus reversing the order of the Key List and the values List.
All of which seems very strange. What am I missing – and why doesn’t Choose do what I would expect?
Thanks
Thanks so much Mike. I did try using Select initially but didn’t find the magic operator # as applied to a Matrix name.
BTW – I don’t think the use of the # operator is in the documentation anywhere (not that I found). The use with a category name to return the position along the category is found in samples and seems to be fairly well known, but the use with a Matrix name in the way you show here is I think a well kept secret…
Hi – It is my understand that choose does not work with lists. I don’t know the programmatic reason, but I rarely use choose or lookup in my models – I always try to make select work. Select is much more powerful and you don’t have to worry about the order of the lookup keys etc:
So try this:
result = select(Lists::A1,#Lists::B,3)
The # operand returns the item position in the category for each value (1,2,3 etc…) so you can use that as the lookup key in select and avoid using a dummy key.
-Mike