This article shows how a list can be represented as a string, where each list item is separated by a character (often called a delimiter).

For example, a list containing the following items:

  • apple
  • orange
  • lime

could be represented as the string apple;orange;lime;, using the delimiter ;.

Script

The following script compiles every item in the list (list::list) into the variable (result).

define compile list into string separated by (delimiter)
set [result v] to []
set [i v] to [1] // counter
repeat (length of [list v]) // enumerate over every item
  set [result v] to (join (join (result) (item (i) of [list v])) (delimiter)) // join the list item along with delimiter
  change [i v] by (1) // increment counter to go to the next item
end
Note Tip: Running this in a custom block set to "run without screen refresh" will make it instant.


How it Works

The variable (i) is stores a number representing a list item.

The script uses a loop (repeat) to continuously add a different value from the list to the string using:

set [result v] to (join (join (result) (item (i) of [list v])) (delimiter::custom-arg))

and the variable (i)'s value is increased by 1 in every iteration of the loop.

List Reporter Method

The (list :: list) block reports the contents of the list as a string. The delimiter is a space, unless the list consists entirely of single characters, in which case there is none.

set [result v] to (list::list)

Decompiling

To go the other way and separate the string into list items, the following script can be used:

decompile [apple;orange;lime;] separated by [;]

define decompile (string) separated by (delimiter)
delete all of [list v] // the result is stored in this list
set [substring v] to [] // the list item is stored here temporarily
set [i v] to [1] // counter
repeat (length of (string)) // enumerate over every character
  if <(letter (i) of (string)) = (delimiter)> then // check if character is a delimiter
    add (substring) to [list v] // add substring to list
    set [substring v] to []
  else
    set [substring v] to (join (substring) (letter (i) of (string))) // join next character onto substring
  end
  change [i v] by (1) // increment counter to go to the next character
end