Paste #8896: A little example of definitions, ternary tags, sorting, and list manipulation.

Date: 2014/08/26 21:14:53 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


Definition_Example:
  type: world
  debug: false
  events:
    on defs command:
    - determine passively fulfilled
    - define args '<tern[<c.args.size.is[MORE].than[0]>]:<c.args> || li@This|is|a|default|sentence|you|silly!>'
    - run locally defs instantly def:%args%

  defs:
    - define defs <queue.definitions>
    # We can use <queue.definitions> at the begining of a new queue to show all
    # the definitions that were passed to it.
    - narrate "<&sp><&3><&n>Queue Definitions"
    - narrate ""
    # To illustrate, the output here is the definition name %def% followed by
    # the definition value <def[%def]>
    - foreach %defs% {
      - define def %value%
      - narrate "<&sp><&sp><&sp><&6>%def%<&co> <&f><def[%def%]>"
      }
    # Not quite the output you would expect. That's because the
    # <queue.definitions> list is built from an internal (to denizen) hashmap
    - narrate ""
    - narrate "*--------"
    - narrate ""

    # Since the list %defs% we are working with was passed from a run command,
    # the definition names are the order index li@1|2|3... as seen above. This
    # is helpfull since the order of the list %defs% was disturbed by using the
    # <queue.definitions> tag. We can sort this list by number order using the
    # <list.numerical> tag.
    - narrate "<&sp><&3><&n>Sorted List - Numeric"
    - narrate ""
    - define sortedDefs <def[defs].numerical>
    # Now that we have out list of definitions back in the correct order, let's
    # reconstruct the sentence you entered.
    - define sentence li@
    # You must create an empty list object so it's available when we try to add
    # data to it!
    # The value being added to the list is the word in your sentence, not the
    # definition name!
    - foreach %sortedDefs% {
      - define def %value%
      - narrate "<&sp><&sp><&sp><&6>%def%<&co> <&f><def[%def%]>"
      - define sentence '<def[sentence].include[<def[%def%]>]>'
      }
    - narrate ""
    # Let's display the list of definition names in sorted order. We do not need
    # a fallback value here because sortedDefs will be a list object, even if it
    # has only one value. This is due to the .numerical tag returning a list.
    - narrate "<&sp><&3>Definition name order reconstructed<&co> <&f><def[sortedDefs].space_separated>"
    - narrate ""
    # Since we've defined %sentence% as a list object, we do not need a fallback
    # here if if you only enter one word in your string.
    - narrate "<&sp><&3>Your string reconstructed<&co> <&f><def[sentence].space_separated>"
    - narrate ""
    - narrate "*--------"