Assigning key-value pairs in a code block

I am developing a snippet that parses an HTML page. The extracted data needs to be transformed in several ways. The typical way to do that in a simple programming language is to use arrays, ordered, unordered and/or associated arrays (key/value lists).

From the documentation, I can't seem to find the syntax for manipulating lists in a code block. Specifically, I would like to know about:

  • Assigning additional key/value pairs to an existing list. (pop and pull functions?)
  • Multiple assignment (adding multiple values in one statement)
  • Multidimensional assignment (can it be done in one go or would I need to loop through loading single-dimension lists and then adding those to a multi-dimensional one?)
  • Addressing of list properties: list length (i.e. item count), all items. (like my_list[*])
  • Variations on these: for ordered lists, non-ordered, key/value, multidimensional
  • Can you mix/match list types in dimensions? An ordered list of sets of three key/value pairs, for instance?
  • Addressing all the keys and/or all the values.

I'm sure most of these operations exist because I see bits and pieces in the answers to questions on the forum, but I think the community as a whole would be helped tremendously by a dose of reference docs. Many thanks!

1 Like

Assigning additional key/value pairs to an existing list. (pop and pull functions?)

In code block you can assign value to keys directly:

{run: x=["a": 1]
x["b"]=2
}
{=x}

Also merge function can be used to merge lists, including keyed lists. For example:

{y=["a": 1, "b": 2, "c": 3]}
{z=["a":444, "d": 4]}
{=merge(y,z)} results in ["a": 444, "b": 2, "c": 3, "d": 4]

Pop can be performed with list comprehension

{run: x=["a": 1]
x["b"]=2
x = [k: v for (k,v) in x if k <> "a"]
}
{=x}

Multiple assignment (adding multiple values in one statement)

Unfortunately that is not possible. More on that: Text Blaze | {=} (Formula Command)

Multidimensional assignment (can it be done in one go or would I need to loop through loading single-dimension lists and then adding those to a multi-dimensional one?)

You can do it in one go. For example assignment for two dimensional array {matrix=[[1, 2], [3,4]]}. Guide for lists of lists: Text Blaze | Interacting with lists

Addressing of list properties: list length (i.e. item count), all items. (like my_list[*])

There is count(mylist) function to get length of a list. Text Blaze | Formula Reference. Could you elaborate on the second part?

Variations on these: for ordered lists, non-ordered, key/value, multidimensional

count can be used for multidimensional lists as well.

Can you mix/match list types in dimensions? An ordered list of sets of three key/value pairs, for instance?

Yes. For example: [["a": 1], ["b": 2]] is a valid syntax

Addressing all the keys and/or all the values.

keys function gets all keys of a keyed list. More on that.

Alternatively list comprehension can be used:

keys   {=[k for (k, v) in y]}
values {=[v for (k, v) in y]}

More on list comprehension: Text Blaze | Formula Reference

2 Likes