Tinderbox Forum

Looping through two containers of notes and manipulating their $Text?

I have a Tinderbox file in which I am planning a course I will be teaching. There is a set of literature references involved, plus my notes on each piece of literature. The references and the notes are each in one container, and (since they were created separately) I want to consolidate them into one set of notes.

Say I want to put the $Text of my comments notes to the end of the $Text of each reference. How would I go about it?

Both sets of notes have the same reference key for each piece of literature, with the reference proper (imported from Bookends) this key being fronted by an @. So assume I have @Anderson2019, @Becker2020 and @Bernstein2021 plus my comments in the notes Anderson2019, Becker2020 and Bernstein2021.

I know I somehow need to loop through the two containers’ contents, checking for equivalence between the note $Name fields (minus the @) and, if it is the case, then add the $Text of say Anderson2019 to the end of the $Text of @Anderson2019.

Any ideas how to do that and code snippets will be gratefully received!

The main glue here is:

$Text = $Text +"\n" + [something textual]

(The \n is a line break to ensure the added text starts a new paragraph.)

So, if the current note is ‘@Anderson2019’ and we want to add the $text of ‘Anderson2019’ to its $Text we could do:

$Text = $Text +"\n"+$Text("Anderson2019");

Or to the code, e.g. for a stamp or agent action:

$Text = $Text + "\n" + $Text($Name.substr(1));

The latter takes a current note title, like ‘@Bernstein2021’, and looks for and adds the $Text of a note ‘Bernstein2021’.

2 Likes

One thing to keep in mind is that you only want to append the text once. Otherwise, you might append some text, and then append it again, and again!

So, we might make a boolean user attribute $Updated, and make this conditional:

if (!$Updated) {
     $Text = $Text + "\n" + $Text($Name.substr(1));
     $Updated=true;
     }
2 Likes

I don’t have the answer just yet, but I could see there being a refinement to this. If you ever update your comments you’ll want the text to be updated. In this case, you’ll want to delete the previous insertion and have it updated with the new one. I’m sure it can be done with a RegEx and modified boolean, but I just thought I’d throw this idea out there as a future consideration.

In this case, I’d add a ‘splitter’ string, e.g. \n~~~~\n or such. That way, the main note $Text can be edited by working above the splitter and the content below can be added to/replaced by parsing for the splitter. @eastgate’s suggestion of a guard Boolean still holds true. The letter tells you when to (not) update, the former assists in what to update.

Many thanks for the kind suggestions to @mwra, @eastgate and @satikusala!
They’re helpful, for first of all I noted that my notion of having to “loop through” a container is obviously mistaken and influenced by experience from other programming languages where you can do FOR… loops. Obviously in Tinderbox agents will do what I want without such constructs - I hope I’m right here.
Secondly, I only need to do this once, so it’s not something that has to be constructed to be repetitive. The note in question (which I then exploded) was written as I went through the whole list of literature, noting down my thoughts on each item I had marked in Bookends.
Perhaps I’ll bring it up in the Meetup later.

The meet-up was hopefully useful but if new issues emerge, here’s the place to raise them. :slight_smile:

1 Like

Participants in today’s meetup were kind enough to help me find and implement a solution to my problem. In case anyone else ever wants to solve a similar problem and stumbles across this thread, here is the solution we found for my problem stated above.

There are basically two ways of doing the merging of information from a related file to the reference file in question:

1.) Do it with a stamp to change the Reference note by adding the $Text of the comment note to it.

$MyString=$Name.substr(1);$Text=$Text + $Text($MyString);$IsMerged=true;

The first command dynamically finds the name of the comment note related to each Reference note (see above - they have the same name, except that that of the Reference note is prefaced with an ‘@’) and stores it in $MyString (one can put it into a permanent attribute if it is to be used further). The substr bit gets rid of the first character (the @) and retains the rest of the name - just what we need.
The second command instructs Tinderbox to go fetch the $Text of the appropriate comment note and add it to the Reference note (whose format is already so that no ‘\n’ is needed).
The third command sets a boolean variable to true to indicate the merger has taken place (to avoid duplication). A possible extension would be to wrap the instructions into an if condition so that action only takes place if IsMerged is false.

2.) Create a template for output of the Reference note(s) so that the merger pulls in $Text from the comment note just for the output (i.e. no change in the Reference note).

This requires adding an HTML output template in which

^include($MyString,"tText")^

is added after ^text^ is invoked.

I prefer the first option for my purposes, but knowing that I can dynamically add content from another note to anything I want to output is a great finding.

Many thanks to all who helped find this! The Tinderbox community is a huge asset for those trying to learn how to get the most out of this exciting software.

3 Likes