This topic describes the syntax, description, parameters, return values, and examples of dictionary functions.

set

This function is described as follows:
  • Syntax: set(d, k, v)
  • Description

    You call this function to set the k/v pairs in a dictionary specified by the d parameter.

  • Parameters
    • d: the name of the dictionary.
    • k: the key. Data type: any type.
    • v: the value. Data type: any type.
  • Return values

    This function returns true.

  • Examples
    • Example 1
      outer_keys=['e66fd4aa-f281-472f-b919-fc7e7474de25', '66fee78d-1887-42ec-9119-a9b50b7fbca2']
      say(concat('keys[1]=', get(outer_keys, 1)))
      say(concat('keys[2]=', get(outer_keys, 2)))
      
      inner_keys=[]
      set(inner_keys, 'dev', '243390eb-00b7-4551-a6b8-021bb34d1674')
      set(inner_keys, 'zeus', '4747d33b-12b0-45e6-ac10-a8e191d6adaa')
      
      def echo_each(k, v, u) {
          s = concat('keys[', k, ']=', v)
          say(s)
      }
      foreach(inner_keys, echo_each, [])
      
      Output:
      keys[1]=e66fd4aa-f281-472f-b919-fc7e7474de25
      keys[2]=66fee78d-1887-42ec-9119-a9b50b7fbca2
      keys[dev]=243390eb-00b7-4551-a6b8-021bb34d1674
      keys[zeus]=4747d33b-12b0-45e6-ac10-a8e191d6adaa                                                                                                                                              
    • Example 2
      d_inner = []
      set(d_inner, 'name', 'inner dsl')
      
      d_outer = []
      set(d_outer, 'dictA', d_inner)
      
      v = get(d_outer, 'dictA')
      if v {
          v = get(v, 'name')
          if v {
              add_rsp_header('X-DSL-NESTED-DICT', v)
          }
      }
      
      Output: Add a response header.
      X-DSL-NESTED-DICT: inner dsl

get

This function is described as follows:
  • Syntax: get(d, k)
  • Description

    You can call this function to retrieve the value (v) corresponding to a key (k) from a dictionary (d).

  • Parameters
    • d: the name of the dictionary.
    • k: the key. Data type: any type.
  • Return values

    This function returns the corresponding value upon a success and returns false upon a failure.

  • Examples
    outer_keys=['e66fd4aa-f281-472f-b919-fc7e7474de25', '66fee78d-1887-42ec-9119-a9b50b7fbca2']
    say(concat('keys[1]=', get(outer_keys, 1)))
    say(concat('keys[2]=', get(outer_keys, 2)))
    
    inner_keys=[]
    set(inner_keys, 'dev', '243390eb-00b7-4551-a6b8-021bb34d1674')
    set(inner_keys, 'zeus', '4747d33b-12b0-45e6-ac10-a8e191d6adaa')
    
    def echo_each(k, v, u) {
        s = concat('keys[', k, ']=', v)
        say(s)
    }
    foreach(inner_keys, echo_each, [])
    
    Output:
    keys[1]=e66fd4aa-f281-472f-b919-fc7e7474de25
    keys[2]=66fee78d-1887-42ec-9119-a9b50b7fbca2
    keys[dev]=243390eb-00b7-4551-a6b8-021bb34d1674
    keys[zeus]=4747d33b-12b0-45e6-ac10-a8e191d6adaa                                                                                                                                              

foreach

This function is described as follows:
  • Syntax: foreach(d, f, user_data)
  • Description
    • You can call this function to traverse elements in a dictionary (d) and execute a callback function (f) for each elements.
    • Specify the f parameter in the syntax of f(key, value, user_data).
    • When the f() function returns false, the foreach() loop is terminated.
  • Parameters
    • d: the name of the dictionary.
    • f: the callback function.
    • user_data: the user data you want to transmit. Data type: dictionary.
  • Return values

    This function always returns true.

  • Examples
    • Example 1
      outer_keys=['e66fd4aa-f281-472f-b919-fc7e7474de25', '66fee78d-1887-42ec-9119-a9b50b7fbca2']
      say(concat('keys[1]=', get(outer_keys, 1)))
      say(concat('keys[2]=', get(outer_keys, 2)))
      
      inner_keys=[]
      set(inner_keys, 'dev', '243390eb-00b7-4551-a6b8-021bb34d1674')
      set(inner_keys, 'zeus', '4747d33b-12b0-45e6-ac10-a8e191d6adaa')
      
      def echo_each(k, v, u) {
          s = concat('keys[', k, ']=', v)
          say(s)
      }
      foreach(inner_keys, echo_each, [])
      
      Output:
      keys[1]=e66fd4aa-f281-472f-b919-fc7e7474de25
      keys[2]=66fee78d-1887-42ec-9119-a9b50b7fbca2
      keys[dev]=243390eb-00b7-4551-a6b8-021bb34d1674
      keys[zeus]=4747d33b-12b0-45e6-ac10-a8e191d6adaa                                                                                                                                              
    • Example 2
      Output the first two M3U8 slices and terminate the foreach loop.
      def echo_each(k, v, u) {
          say(v)
      
          if match_re(v, '.*ts') {
              ts_cnt = get(u, 'ts_cnt')
              ts_cnt = add(ts_cnt, 1)
              set(u, 'ts_cnt', ts_cnt)
      
              if ge(ts_cnt, 2) {
                  return false
              }
          }
      }
      
      m3u8 = ''
      m3u8 = concat(m3u8, '#EXTM3U8', '\n')
      m3u8 = concat(m3u8, '#EXT-X-MEDIA-SEQUENCE:140651513\n')
      m3u8 = concat(m3u8, '#EXT-X-TARGETDURATION:10\n')
      m3u8 = concat(m3u8, '#EXTINF:8,\n')
      m3u8 = concat(m3u8, 'http://vapp1.fw.live.cntv.cn/cache/289_/seg0/index140651514_140651513.ts\n')
      m3u8 = concat(m3u8, '#EXTINF:9,\n')
      m3u8 = concat(m3u8, 'http://vapp1.fw.live.cntv.cn/cache/289_/seg0/index140651514_140651514.ts\n')
      m3u8 = concat(m3u8, '#EXTINF:10,\n')
      m3u8 = concat(m3u8, 'http://vapp1.fw.live.cntv.cn/cache/289_/seg0/index140651514_140651515.ts\n')
      
      lines = split(m3u8, '\n')
      u = []
      set(u, 'ts_cnt', 0)
      foreach(lines, echo_each, u)
      
      Output:
      #EXTM3U8
      #EXT-X-MEDIA-SEQUENCE:140651513
      #EXT-X-TARGETDURATION:10
      #EXTINF:8,
      http://vapp1.fw.live.cntv.cn/cache/289_/seg0/index140651514_140651513.ts
      #EXTINF:9,
      http://vapp1.fw.live.cntv.cn/cache/289_/seg0/index140651514_140651514.ts