Tinderbox Forum

Extract elements in $Text

If I had a note with text in it and I wanted to extract only those lines in the text that started with a "- " and then place those extractions into another note’s $Text. How would I do that?

This is what the new stream parsing tools are for an with which I’m not export (so, no code!). Another method is to iterate the source $Text paragraphs, test the opening of the paragraph text and move accordingly.

$Text.paragraphList.each(aPara){
   if(aPara.beginsWith("-")){
      // do whatever...
   }
};

As I say, I think we ought to be using Stream parsing for this. Perhaps spomeone has some code to share?

[Edit] footnote: we might easily intuit $Text.paragraphs.each() as the syntax. However, paragraphList is needed because .parargraphs(N) already existed and has a different purpose.

Thanks. Ya, I don’t have a clue.

If I have this:

Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum 

- one
- two
- three

I would like to be able to parse the text and capture the one to three as separate lines in another text. I tried figuring out the Stream. But, have no clue how to make it work.

Here’s a quick test:

Result:

Here’s the file: Para-handler.tbx (81.0 KB)

Edit: For easy comparison with other methods, this is the $Edict from note “Source” in the above TBX:

$Text("Target")=;
$Text.paragraphList.each(aPara){
   if(aPara.beginsWith("-")){
      $Text("Target")+=aPara+"\n";
   }
};

(Here, note “Source” is the note holding the $Text we wish to check, and “Target” is the note in which we store the matching sub-texts. Of course, that method can be varied many ways, e.g. a different ‘target’ note per sub-string, even a new note for each if needed (though that’s left as an exercise for or specific question from the reader).

1 Like

Thanks! Is there a way to do this with stream? Anyone know?

$Text=""; 
$Text(/Target).eachLine(x){    
     if(x.beginsWith("-")) {$Text+=x+"\n";}
 }
2 Likes

I added the edict used in my demo to my previous post so the two methods can be compared. So for this, comparatively simple, stream parse Stream.eachLine() substitutes one operator for two: $Text.paragraphList.each()$Text.eachLine().

I’ve just updated the aTbRef notes for .paragraphList() and .eachLine() to reflect this newer shorter usage. How quickly things change round here: the former operator only arrived in v9.0.0 and the latter in v9.1.0. Tinderbox—always improving. :slight_smile: