question

martin.j avatar image
0 Likes"
martin.j asked Matthew Gillespie commented

Number of labels & labels by numeric reference

With the new dot syntax for labels it has become pretty easy to access a label by name, but is it still possible to access it by number? If I want to get the last label on an item, I would have to know how many labels the item has in total and be able to use that as the index.

But apparently the LabelsArray datatype does not support the Length property nor will it accept anything other than a string name. It doesn't seem to be possible to get a label by its index unless I use the deprecated getlabels functions.

So how can I get the total number of labels on an object?

And how can I access the label with its index?

labelsdot syntax
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Joerg Vogel avatar image
2 Likes"
Joerg Vogel answered Matthew Gillespie commented

You mean this

obj.attrs.labels.subnodes[num rank]

?


· 6
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Patrick Cloutier avatar image Patrick Cloutier commented ·

Hello, I'm trying to do the same which is referencing labels by index but on token labels.

I've tried these but they don't work:

print(token.labels[3].value);
print(token.attrs.labels[3].value);
print(token.attrs.labels.subnodes[3].value);

What am I missing?

Thanks,

0 Likes 0 ·
Matthew Gillespie avatar image Matthew Gillespie ♦♦ Patrick Cloutier commented ·

@Patrick Cloutier

Accessing token labels by rank is not officially supported because not all the labels a token can read or write are stored on the token itself. Many times these token labels are stored on the token's parent or grandparent token.

3 Likes 3 ·
Joerg Vogel avatar image Joerg Vogel Patrick Cloutier commented ·

@Patrick Cloutier, this question is related to deleting labels at a token, because you want to get access to the node structure. I came up with a source code to achieve this in my answer here. From this code is it not to far to get what you want. The main hook is to cast the token into a treenode to get access to the treenode structure.

0 Likes 0 ·
Patrick Cloutier avatar image Patrick Cloutier Joerg Vogel commented ·

Thanks Jorg.

0 Likes 0 ·
Joerg Vogel avatar image Joerg Vogel commented ·

Or are looking for

obj.attrs.labels.find("/1");

?

obj.attrs.labels.last.name;
-1 Like -1 ·
martin.j avatar image martin.j Joerg Vogel commented ·

Thanks Jörg, its so obvious now that I see it that I feel silly for asking. Thank you for taking the time to answer.

0 Likes 0 ·