All Products
Search
Document Center

CDN:Dictionary functions

Last Updated:Apr 01, 2026

Dictionary functions let you create, read, update, and delete key-value pairs in EdgeScript dictionaries. Use these functions to pass structured data between script sections or to store intermediate computation results within a single request.

In EdgeScript, declare a dictionary as an empty array literal ([]) and populate it with set().

d = []
set(d, 'key', 'value')

set

Sets a key-value pair in a dictionary.

Syntax

set(d, k, v)

Parameters

ParameterTypeDescription
danyThe dictionary to write to.
kanyThe key.
vanyThe value to set.

Return value

Always returns true.

Examples

Example 1: Set string keys and iterate with foreach.

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, [])

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: Use nested dictionaries to store structured data and retrieve values at multiple levels.

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 — a response header is added:

X-DSL-NESTED-DICT: inner dsl

get

Retrieves the value associated with a key from a dictionary.

Syntax

get(d, k)

Parameters

ParameterTypeDescription
danyThe dictionary to read from.
kanyThe key to look up.

Return value

Returns the value associated with k if the key exists. Returns false if the key is not found.

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, [])

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

Iterates over every entry in a dictionary and calls a callback function for each key-value pair.

Syntax

foreach(d, f, user_data)

Parameters

ParameterTypeDescription
danyThe dictionary to iterate over.
ffunctionThe callback function. Must follow the signature f(key, value, user_data).
user_datadictionaryArbitrary data passed through to each callback invocation.

Return value

Always returns true.

Usage notes

  • The callback f must use the exact signature f(key, value, user_data).

  • Return false from f to stop iteration early. The loop exits immediately when the callback returns false.

Examples

Example 1: Print all key-value pairs in a dictionary.

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, [])

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: Parse an M3U8 playlist and stop after printing the first two .ts segments.

The callback tracks a counter in user_data and returns false once two .ts segments have been printed, ending the loop early.

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)

Output — the loop stops after the second .ts segment:

#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 a key-value pair from a dictionary.

Syntax

del(d, k)

Parameters

ParameterTypeDescription
danyThe dictionary to delete from.
kanyThe key to delete.

Return value

Always returns true.

Example

Check for a key, delete it, then verify it is gone.

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)
}

Output — the first check finds the key, and the second confirms it was deleted:

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