lastmost

operator

Syntax
shelf lastmost


Purpose

lastmost can be used to identify a shelf item to be accessed, wherever an item indexer, [...] or key {...} can be used. The lastmost indexer always accesses the last item on the shelf.

Note that the syntaxes shelf lastmost and shelf[number of shelf] are identical when used to access an item on a shelf. They behave differently when they are used to set the currently selected item:

  • when the shelf is being indexed in a using prefix
  • when the indexed shelf is being passed to a function as a modifiable or read-only argument

In the two cases above, when the default item of the shelf or function argument is accessed, the lastmost item is recalculated, while number of is not. This means that when the default item is set to lastmost, it will always be the last item on the shelf, regardless of how the shelf changes.

When the default item on a shelf is set to lastmost, it will always be the last item on the shelf regardless of how the shelf changes. When used to set the currently selected item, lastmost behaves differently than number of.

In this example, which uses lastmost,

  process
     local integer c variable
  
     clear c
     set new c to 1
     using c lastmost
        do
           output "The value is %d(c)%n"
           set new c to 2
           output "The value is %d(c)%n"
           set new c to 3
           output "The value is %d(c)%n"
        done

the output is:

  The value is 1
  The value is 2
  The value is 3

However, when the default is set to the last item with "[number of c]":

  process
     local integer c variable
  
     clear c
     set new c to 1
     using c [number of c]
        do
           output "The value is %d(c)%n"
           set new c to 2
           output "The value is %d(c)%n"
           set new c to 3
           output "The value is %d(c)%n"
        done

the output is:

  The value is 1
  The value is 1
  The value is 1

This is because the number of items on shelf c is just 1. That becomes the numeric index of the shelf for the duration of using.

Note that it cannot be assumed that the "lastmost" item of a passed shelf is its current item. The following function takes this possibility into account, and the using keyword is used to ensure that references to the stream code-set refer only to the newly created item.

  define function add-new-codes (modifiable stream code-set,
                                 value string new-codes) as
     using code-set lastmost
     do
        new code-set
        open code-set as buffer
        repeat scan new-codes
        match letter => one-code
           put code-set "%ux(one-code)"
        match any
           ; skip
        again
        close code-set
     done

Related Syntax