Help on two tasks for data-tiddlers: Adding all values and removing all values

As said in the title: I would need a quick help for two tasks: Adding and removing all values of a data tiddler- the latter with a button. Thank you very much.

If you want to remove “all” values from a data-tiddler, just delete that tiddler. That’s a simple operation.
See: https://tiddlywiki.com/#ActionDeleteTiddlerWidget

You will need to allow your logic to handle a “non existent” data-tiddler in the right way with “fallback” values. That’s basically it - for deleting.

I am not sure about adding “all” values – What are “all” values. Can you be more specific here.

2 Likes

If you want to retain the data-tiddler and delete all the index names/values, you can write something like this:

<$let tid="DataTiddlerTitle">
<$button> clear all indexes <$action-setfield $tiddler=<<tid>> text=""/> </$button>

This should work for “application/json” or “application/x-tiddler-dictionary” type tiddlers, as in both cases, the indexes and values are stored in the tiddler’s text field.


On the other hand, if you want to retain the data-tiddler AND keep all the index names, but only clear the stored values, you can write something like this:
<$let tid="DataTiddlerTitle">
<$button> clear all values
<$list filter="[<tid>indexes[]]" variable=thisindex>
   <$action-setfield $tiddler=<<tid>> $index=<<thisindex>> $value=""/> 
</$list>
</$button>

This will set each index value to blank without removing the index itself.


Regarding "adding all values"... do you mean computing the sum of all the values, or do you mean creating/setting multiple indexes? If you meant "computing the sum", then you can do that like this:
<$let tid="DataTiddlerTitle">
{{{ [<tid>indexes[]] :map[<tid>getindex<currentTiddler>] +[sum[]] }}}

The first filter run gets the names of all the indexes contained in the data tiddler.
The :map[...] filter run gets the corresponding stored value for each index.
The last filter run computes the total for all the retrieved values.

-e

4 Likes

Thank you @EricShulman , keeping the names is what I want as you guessed.

Using your trick made another problem apparent:
My data-tiddlers are handmade and not very clean. sometimes the lines miss the colon :
Using the button then deletes all these unfinished lines.

Is there a trick to add a : at the end of each line if it is missing.

I tried it with a regexp but I did not get the correct spell yet.

\define regex() ^([^:\n]*)$

<$tiddler tiddler="Data:Musterlösung Präsidialkabinette">
<$button> close all lines
   <$action-setfield $tiddler=<<currentTiddler>> text={{{[{!!text}search-replace:regexp:gm<regex>,[$1:]]}}}/> 
</$button>

</$tiddler>

Is your tiddler “application/json” or “application/x-tiddler-dictionary”?
Can you paste an example of some mal-formed data tiddler contents here?

-e

1 Like

x-tiddler-dictionary.
It is much easier to Create a sample solution
manally that way.

Your regex “spell” is correct… the problem is that you have the suffixes for the search-replace backwards!

It should be search-replace:gm:regexp<regex>

-e

1 Like

Hi Eric, I’m not sure if you have insight into this, but having multiple suffixes with filter operators is pretty rare - I can only think of a couple, and at least I find them a bit more confusing than normal. I have to imagine that there’s been some debate over whether to have these multiple suffixes or to have separate filter operators. Especially with multiple, optional suffixes that are positional, there must be some complex parsing happening.

To compare how it is with what I think is a simpler alternative:

# Current multi-suffix
[search-replace:<flag list>:<regexp-mode>[<search-term>],[<replacement>]]

# Regular search-replace
[search-replace:<flag list>[<search-term>],[<replacement>]]

# Regexp search-replace
[regexp-replace:<flag list>[<search-term>],[<replacement>]]

I only bring it up as if we did want to consider simplifying the operators that are multi-suffix, a 5.4.0 would be the perfect time to do it. Just a thought on how we could simplify things. Maybe it’s just me that gets a little confused with these!

1 Like

Thank you! I was rewriting the regExpression all the time.