slice it up

Missing values in a Coda list

Slicing the array, using an index and taking the last item

Christiaan Huizer

--

I came across a little puzzle in the community, the solution of which was provided by its creator. This blog post explores the solution and offers further context. The reason I write a blog about it is that although the solution may feel simple, it is not. Chapeau for the maker!

For those not familiar with the functions Slice() and Find() some context. The slice() function was introduced around the late 1990s and early 2000s to provide a standardized and versatile way to work with arrays in JavaScript (arrays are lists).

Coda switched from using the term “Array” to “List()” to describe a list of comma-separated items. Array is the term you’ll notice in other programming languages as well like in Python.

The slice() method was designed to extract a portion of an array without modifying the original array. This is useful for creating copies of array segments or for working with subsets of data. It is always about the position of the items in the array (list) and here we have a link to the function Find().

Find() in thisTable context

While the image shows Coda and thus not pure JavaScript, it is using the function Find() with the same logic as in JavaScript. Here, we leverage Find() to determine the position of thisRow within a table. This is a frequently used pattern to generate an index, as demonstrated in the "index" column of our example:

thisTable.Find(thisRow)

This snippet, as seen in the “Column formula” popup, returns the row number (index) of the current row within thisTable.

Slicing the Data

The Slice() function, also visible in the formula, then carves out a portion of our list with items. In this specific solution, we start slicing from position 1 and go up to the index returned by Find(). This creates the sequence observed in the "slice it" column: 1,1 (because we slice from 1 to the index which is 1) then 1,2, then 1,3, then 1,4, and so on.

demo the slice index

Why is slicing this list so tricky?

For Row 4 (where “Name” is B), Find() returns 4. Slice(1, 4) extracts elements from index 1 up to index 4 but not from the list [A, B, C, B] as you perceive it. On purpose I colored the letters to avoid that you would believe that the second B is equal to the last B. What you don’t see in the list[A, B, C, B] is that it contains empty items, so called blanks: A, , , B, , C, , B, which I show in ‘the real list’ below. I got here by adding start.ToText() and only now we understand why 1.4 results in the A,B.

how we easily are mislead by our eyes

Once we understand that we are dealing with a list that contains empty items (blanks), we also understand that we have to get rid of these empty items.

The maker applied a more technical syntax: Filter(CurrentValue!=“”) which I prefer to rewrite in CFL as Filter(CurrentValue.IsNotBlank()) outputting the same result. It makes sure that we get only the items that are not blank and from this list we take each time the last.

So here we are, understanding that the list we created and needed contained blanks which came in handy for our slice and find approach, but in the end we filter on items that are not blank and we take the last as you see below.

the solution

The demo doc you find here.

Hopefully, this post has shed some light on this common Coda challenge and empowered you to create more interactive and insightful data experiences. On a personal note, creating these in-depth posts takes a lot of time and effort. While I love sharing my knowledge, a little support goes a long way. If you found this helpful, what about a donation or sharing this post with your fellow Coda enthusiasts? Every bit of encouragement helps fuel the next 47 blockbusters!

My name is Christiaan, and I regularly blog about Coda. While this article is free, my professional services (including consultations) are not, but I’m always happy to chat and explore potential solutions. You can find my free contributions in the Coda Community and on X. The Coda Community is a fantastic resource for free insights, especially when you share a sample doc.

--

--

Christiaan Huizer
Christiaan Huizer

Written by Christiaan Huizer

I write about how to Coda . You find blogs for beginners and experienced makers. I publish about 1 / week. Welcome!

No responses yet