All Products
Search
Document Center

CDN:Dictionary functions

Last Updated:Aug 24, 2026

This topic describes the syntax, description, parameters, and response parameters of dictionary functions. This topic also provides examples of these functions.

set

Use the set function to add or update a key-value pair in a dictionary.

Item

Description

Syntax

set(d, k, v)

Parameters

  • d: The target dictionary.

  • k: The key to add or update. Data type: Any.

  • v: The value to associate with the key. Data type: Any.

Examples

  • Example 1

    outer_keys=['e66fd4aa-f281-472f-b919-fc7e7474****', '66fee78d-1887-42ec-9119-a9b50b7f****']
    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-021bb34d****')
    set(inner_keys, 'zeus', '4747d33b-12b0-45e6-ac10-a8e191d6****')
    
    def echo_each(k, v, u) {
        s = concat('keys[', k, ']=', v)
        say(s)
    }
    foreach(inner_keys, echo_each, [])                                                                                                                                             
  • 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)
        }
    }

Return value

This function always returns true. The examples produce the following output:

  • Example 1 output:

    keys[1]=e66fd4aa-f281-472f-b919-fc7e7474****
    keys[2]=66fee78d-1887-42ec-9119-a9b50b7f****
    keys[dev]=243390eb-00b7-4551-a6b8-021bb34d****
    keys[zeus]=4747d33b-12b0-45e6-ac10-a8e191d6****
  • Example 2 adds the following response header:

    X-DSL-NESTED-DICT: inner dsl

get

Use the get function to retrieve the value associated with a specified key from a dictionary.

Item

Description

Syntax

get(d, k)

Parameters

  • d: The target dictionary.

  • k: The key of the value to retrieve. Data type: Any.

Example

outer_keys=['e66fd4aa-f281-472f-b919-fc7e7474****', '66fee78d-1887-42ec-9119-a9b50b7f****']
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-021bb34d****')
set(inner_keys, 'zeus', '4747d33b-12b0-45e6-ac10-a8e191d6****')

def echo_each(k, v, u) {
    s = concat('keys[', k, ']=', v)
    say(s)
}
foreach(inner_keys, echo_each, [])                                                                                                                                             

Return value

If the key exists, the function returns its corresponding value. Otherwise, it returns false. The example produces the following output:

keys[1]=e66fd4aa-f281-472f-b919-fc7e7474****
keys[2]=66fee78d-1887-42ec-9119-a9b50b7f****
keys[dev]=243390eb-00b7-4551-a6b8-021bb34d****
keys[zeus]=4747d33b-12b0-45e6-ac10-a8e191d6****

foreach

The foreach function has the following behaviors:

  • It iterates over each key-value pair in the dictionary d and executes the callback function f.

  • The callback function f must have the signature f(key, value, user_data).

  • The foreach() loop terminates if the f() callback function returns false.

Item

Description

Syntax

foreach(d, f, user_data)

Parameters

  • d: The target dictionary.

  • f: The callback function to execute for each key-value pair.

  • user_data: Custom user data to pass to the callback function. This parameter must be a dictionary.

Examples

  • Example 1

    outer_keys=['e66fd4aa-f281-472f-b919-fc7e7474****', '66fee78d-1887-42ec-9119-a9b50b7f****']
    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-021bb34d****')
    set(inner_keys, 'zeus', '4747d33b-12b0-45e6-ac10-a8e191d6****')
    
    def echo_each(k, v, u) {
        s = concat('keys[', k, ']=', v)
        say(s)
    }
    foreach(inner_keys, echo_each, [])                                                                                                                                             
  • Example 2

    This example prints the first two M3U8 segments and shows how to 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:14065****\n')
    m3u8 = concat(m3u8, '#EXT-X-TARGETDURATION:10\n')
    m3u8 = concat(m3u8, '#EXTINF:8,\n')
    m3u8 = concat(m3u8, 'http://***.cn/cache/289_/seg0/index140651514_1406****.ts\n')
    m3u8 = concat(m3u8, '#EXTINF:9,\n')
    m3u8 = concat(m3u8, 'http://***.cn/cache/289_/seg0/index140651514_1406****.ts\n')
    m3u8 = concat(m3u8, '#EXTINF:10,\n')
    m3u8 = concat(m3u8, 'http://***.cn/cache/289_/seg0/index140651514_1406****.ts\n')
    
    lines = split(m3u8, '\n')
    u = []
    set(u, 'ts_cnt', 0)
    foreach(lines, echo_each, u)

Return value

This function always returns true. The examples produce the following output:

  • Example 1 output:

    keys[1]=e66fd4aa-f281-472f-b919-fc7e7474****
    keys[2]=66fee78d-1887-42ec-9119-a9b50b7f****
    keys[dev]=243390eb-00b7-4551-a6b8-021bb34d****
    keys[zeus]=4747d33b-12b0-45e6-ac10-a8e191d6****
  • Example 2 output:

    #EXTM3U8
    #EXT-X-MEDIA-SEQUENCE:140651513
    #EXT-X-TARGETDURATION:10
    #EXTINF:8,
    http://***.cn/cache/289_/seg0/index140651514_1406****.ts
    #EXTINF:9,
    http://***.cn/cache/289_/seg0/index140651514_1406****.ts

del

Deletes key-value pairs from a dictionary (d). The following tables describes the details about this function.

Item

Description

Syntax

del(d, k)

Parameters

  • d: the name of the dictionary.

  • k: the key. Data type: any type.

Examples

var_a = []
set(var_a, 'note_a', 'note a info')
if get(var_a, 'note_a') {
    add_rsp_header('X-RESPOND-OUTPUT', 'found var_a key', true)
} else {
    add_rsp_header('X-RESPOND-OUTPUT', 'del var_a key', true)
}
del(var_a, 'note_a')
if get(var_a, 'note_a') {
    add_rsp_header('X-RESPOND-OUTPUT', 'found var_a key', true)
} else {
    add_rsp_header('X-RESPOND-OUTPUT', 'del var_a key', true)
}

Response parameters

Returns a value of true. Response parameter for this example:

X-RESPOND-OUTPUT: found var_a key
X-RESPOND-OUTPUT: del var_a key