Solved

# How can I pick the first and last item from various lists?

• 2 replies
• 96 views

I’m looking for help to extracting the first and last value of a given list.

I have a binary parameter L(p,i) which will be given varios list of data.

For example:

List p=1:

i=1 → 0

i=2 → 1

i=3 → 1

i=4 → 0

List p=2:

i=1 → 1

i=2 → 0

i=3 → 1

i=4 → 1

I’m looking for a command which will extract the first i which is set to 1 on a given list. In this Example for the list p=1 it would be i=2 and for the list p=2 it would be i=1.

I’m also looking for a command which will extract the last i which is set to 1 on a given list. In this example for the list p=1 it would be i=3 and for the list p=2 it would be i=4.

I tried the “First” and “Last” command which always lead to using i=1 (for the command “First”) even though in the example p=1 the value of i=1 is 0 and not 1.

Any help would be appreciated.

### 2 replies

Userlevel 5
+7

What you could do is create an element parameter that has the range attribute set to the set that corresponds to the index i and that is indexed over index p.

You can then give this element parameter the following definition:

first( i | L(p,i) )

(or last in case of the second question).

You can read this definition as “The first i SUCH THAT there is a non-zero value for L(p,i)”

Thanks a lot for the answer, that worked perfect for my problem.