has key

operator

Return type:
Switch
Returns:
Returns true if the shelf contains a key with the given name, and false otherwise. Replacing has with hasnt will reverse the results.
Syntax
shelf (has | hasnt) key string-expression
or
specified? attributes (has | hasnt)  key string-expression
or
specified? data-attributes (has | hasnt) key string-expression
    


Purpose

has key is a test that determines if a shelf contains a key with the given string-expression.

hasnt key returns the opposite results.

has key can also be applied to an attributes shelf in a similar fashion.

The has key test can also determine whether an element has an attribute declared for it. It can be used on both the data-attributes and attributes shelves.

For example, the following code shows how has key can be used to test for a declared attribute:

  global stream id-name
  
  element #implied
     output attributes key id-name
        when attributes has key id-name & attribute{id-name} isnt implied
     ; ...
          

In this example, the has key test is used to determine whether the switch shelf assoc-list currently contains an item with the name referred to by curr-key:

  global integer item-number
  global switch  associations variable initial { true  with key "black", 
                                                 false with key "gold", 
                                                 true  with key "red" }
  
  process
     submit "{gold}foo-stuff for report"
  
  find "{" [ \ "}"]+ => curr-key "}"
     do when associations has key curr-key
        set item-number to item of associations{curr-key}
  
     else
        ; An index of 0 means that the key wasn't found.
        set item-number to 0
     done
     output "item-number = " || "d" % item-number || "%n"
  ; Output: "item-number = 2
  ;          foo-stuff for report"
          

Related Concepts