How to refer to a child note by name


(Ben Schlingelhof) #1

I want to use

sum_if(children(XXX), $Attribute==“Value”,1)

to count some of the children of note XXX. Now: XXX is not unique. I want to use the code in XXX’s parent’s action, but there might be another XXX somewhere else in the document. I also don’t know the absolute path of XXX. In other situations I can use

$Path/XXX

to refer to a specific child by name but sum_if doesn’t seem to understand that.
Any ideas?

Cheers


(Galen Menzel) #2

This is a bit of a strange one. The following action will work:

var xxxPath;
xxxPath = $Path + "/XXX";
sum_if(children(xxxPath),$Attribute=="Value",1);

But neither of these work:

sum_if(children($Path + "/XXX"),$Attribute=="Value",1);
$MyString = $Path + "/XXX";
sum_if(children($MyString),$Attribute=="Value",1);

My guess is that the argument to children cannot be a full Tinderbox expression, but only a literal string, and that furthermore, Tinderbox vars are fully evaluated to literal strings as part of the action-code evaluation. But @eastgate is the one to say for sure.


(Mark Anderson) #3

Not tested but this my be a situation where eval() is needed, i.e. something like sum_if(children(eval($Path + "/XXX")),$Attribute=="Value",1);. The logic here being that eval generates a string literal that works in offset reference to children.


(Galen Menzel) #4

Doesn’t seem to work in my tests. I wouldn’t expect it to, if my understanding is correct, since eval(...) is also a Tinderbox expression, so if children needs a literal string here eval won’t help matters.


(Ben Schlingelhof) #5

Thanks, the extra var declaration did the trick for me. Tinderbox certainly is a strange beast but I’m slowly finding my way.


(eastgate) #6

A full path always begins with a /. So

sum(children(/Notes), $Width)

is the width of all the children inside the top-level container named Notes.


(Galen Menzel) #7

@eastgate, given that I have the path of the note that I want in an attribute, is assigning that attribute to an intermediate var the only way to use that path in a designator? I.e.,

var path;
path = $Attribute;
sum(children(path), $Width)

Or is there a more direct method?


(eastgate) #8

When we write

sum(children(expression),…).

things get tricky. When we evaluate the expression, to what is this bound? this is bound in sum(), sum_if(). find(), collect() and related constructs to the note that is being examined, not to the note that is performing the rule or action, so $Path is the path to that note, not the path to the action note!

You want

sum(children($Path(that)), …)


(Mark Anderson) #9

Aha! Thanks for that really helpful clarification, which I’ll try and weave appropriately into the aTbRef** notes.

** currently dark due to DNS snafu, but to return.