Return the values of a list of field names?

Hi everyone. I’ve got myself stuck on a problem that feels like it should be quite simple.

I have a MealPlan tiddler, that contains fields name lunch-1, lunch-2, etc. The values of the fields are recipe tiddlers, which themselves contain a field called servings, which has an integer value.

I want to find out the total number of servings in my meal plan, stored in a variable so that I can use it to make other conditional decisions. In other words get the recipe title from each lunch-n field, then get the servings value for each of those recipes, and sum them.

I can get the field titles with {{{ [all[current]fields[]prefix[lunch]] }}}, but then I can’t figure out how to get the values of those fields! It seems like it should be simple but I’m drawing a blank.

Any help would be appreciated! :slight_smile:

I can give a more complete answer tomorrow, its late here, but I suspect all you need is a list within a list. You can do it in a single filter with more work.

Give this a try:

<$let totalservings={{{
   [<currentTiddler>fields[]prefix[lunch]]
   :map[<..currentTiddler>get<currentTiddler>]
   +[get[servings]sum[]]
}}}>
  • The filter has three filter “runs”:
    • First get all fieldnames that begin with “lunch” (which you had already figured out)
    • Next, use :map[...] to replace those names with their corresponding values
      (i.e., the names of the recipe tiddlers). Within :map filter run,
      • <..currentTiddler> is the name of tiddler in which the filter code exists, and
      • <currentTiddler> is value derived by the preceding filter run (i.e., a “lunch…” fieldname)
    • Thus, after the second filter run, you have a list of recipe tiddlers
    • The last filter run then gets the number of servings from each recipe tiddler, and uses sum[] to get the total value

enjoy,
-e

edit: you don’t need the :flat variant of the :map run. Just :map[...] will work as desired.

4 Likes

Thank you so much Eric!