• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

SDK: Get a list of childless keywords

Community Beginner ,
Oct 23, 2021 Oct 23, 2021

Copy link to clipboard

Copied

SDK: My keywords are organized in a hierarchy. Now I want to create a table with the names of all leaf keywords, i.e. keywords without children, end nodes. I understand that I need to call catalog:getKeywords() to get all top-level keywords and then use lrKeyword:getChildren() recursively to get my leaf keywords. However, for some reason recursivity turns my brain to jelly. The recursive function I'm looking for is probably no more than 4 or 5 lines long, but I just can't get it right. Is there a kind soul out there who can help me out?

Also, I'm a bit worried about how to do this once I have that function. The function will have to use startAsyncTask in order to use catalog:getKeywords() and lrKeyword:getChildren(), so how can I be sure that the list of leaf keywords returned by the recursive function is actually available when I want to use if in the calling script?

 

 

 

 

TOPICS
SDK

Views

138

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

LEGEND , Oct 24, 2021 Oct 24, 2021

Here's a function that returns an array of all the lef keywords:

local function findLeafKeywords ()
    local leafKeywords = {}

    local function find (keyword)
        if #keyword:getChildren () == 0 then
            table.insert (leafKeywords, keyword)
        else
            for _, child in ipairs (keyword:getChildren ()) do
                find (child) 
                end
            end
        end

    for _, keyword in ipairs (catalog:getKeywords ()) do find (keyword) end
    return le
...

Votes

Translate

Translate
LEGEND , Oct 24, 2021 Oct 24, 2021

"Is there a way to set the focus to the edit control of  my choice? I can't find any mention of this in the API docs."

 

Unfortunately note.

Votes

Translate

Translate
LEGEND ,
Oct 24, 2021 Oct 24, 2021

Copy link to clipboard

Copied

Here's a function that returns an array of all the lef keywords:

local function findLeafKeywords ()
    local leafKeywords = {}

    local function find (keyword)
        if #keyword:getChildren () == 0 then
            table.insert (leafKeywords, keyword)
        else
            for _, child in ipairs (keyword:getChildren ()) do
                find (child) 
                end
            end
        end

    for _, keyword in ipairs (catalog:getKeywords ()) do find (keyword) end
    return leafKeywords
    end

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 24, 2021 Oct 24, 2021

Copy link to clipboard

Copied

You are the best!

Recursivity really messes with my mind. Even with your code right in front of me I have difficulty in wrapping my mind around it 😉

Anyway, it all works and i'm happy.

Thank you!

 

By the way, I have a dialog box with 9 edit controls (the leaf keyword names actually serve as the edit controls' "completion" property for autocompletion). Is there a way to set the focus to the edit control of  my choice? I can't find any mention of this in the API docs.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 24, 2021 Oct 24, 2021

Copy link to clipboard

Copied

"The function will have to use startAsyncTask in order to use catalog:getKeywords() and lrKeyword:getChildren(), so how can I be sure that the list of leaf keywords returned by the recursive function is actually available when I want to use if in the calling script?"

 

Invoke the main function of the script from startAsyncTask() -- then everything it does will be done serially in that separate task, and you won't have to worry about multiple tasks trying to synchronize.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 24, 2021 Oct 24, 2021

Copy link to clipboard

Copied

"Is there a way to set the focus to the edit control of  my choice? I can't find any mention of this in the API docs."

 

Unfortunately note.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 24, 2021 Oct 24, 2021

Copy link to clipboard

Copied

LATEST

Thanks again. I'm beginning to see what's possible and whar's not in this API. O hope you don't mind all the questions.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines