chavez
chavez

Reputation: 1

lua recursive function gets true and then false within a false

not sure what is happening. i have a recursive function getting data from a table. and it's finds an key to be true and then false twice within it being false..

i am wanting to check the isActive boolean and if it's false return false. if it's true then continue the script.

DUMMY_DATA

    local DummyData = {
    data = {
        ['id'] = 34523456,
        ['question'] = 'whats milk?',
        ['isActive'] = true,
        ['questionCountdownTimerInSeconds'] = (60),
    }
}

RECURSIVE

    function FindQuestionInfo(Object)
    local Data = {
        ['id'] = '',
        ['question'] = '',
        ['isActive'] = true or false,
        ['questionCountdownTimerInSeconds'] = (0),
    }

    for index, child in pairs(Object) do
        
        local ChildIsTable = type(child) == 'table'
        if not ChildIsTable then
            local isActive = index == 'isActive'
            
            local isId = index == 'id'
            local isQuestion = index == 'question'
            local isQuestionCountDDownTImerInSeconds = index == 'questionCountdownTimerInSeconds'

            if isQuestion then
                Data['question'] = child
            end

            if isId then
                Data['id'] = child
            end
        end
        
        if ChildIsTable then
            local FoundItem = FindQuestionInfo(child)
            if FoundItem then
                return FoundItem
            end
        end
    end

    return Data
end

PRINT

printIMAGE

Upvotes: 0

Views: 72

Answers (1)

Piglet
Piglet

Reputation: 28950

Your code doesn't make too much sense. I'm not even sure what you want to achieve with it.

I'll just mention a few issues:

['isActive'] = true or false

As Nifim already pointed out in his comment true or false equals true. Sou you could simply do

['isActive'] = true

You don't need parenthesis around numbers as in ['questionCountdownTimerInSeconds'] = (0)

You don't mention how you use this code. I assume you call FindQuestionInfo(DummyData)

So let's run your code. First you define Data

local Data = {
        ['id'] = '',
        ['question'] = '',
        ['isActive'] = true or false,
        ['questionCountdownTimerInSeconds'] = (0),
    }

Then you traverse over the table Object with a generic for loop and the pairs iterator. Assuming Object is DummyData this will give us a key value pair of DummyData each cycle.

First you check if child (our value) is a table. I don't see how it can be a table with the provided code. If it is not a table you create various booleans.

 local isActive = index == 'isActive'
        
 local isId = index == 'id'
 local isQuestion = index == 'question'
 local isQuestionCountDDownTImerInSeconds = index == 'questionCountdownTimerInSeconds'

And then you assign values conditionally.

if isQuestion then
     Data['question'] = child
   end
   if isId then
     Data['id'] = child
   end

So only if index equals one of the keys you assign the same table field from Object to Data.

This whole loop doesn't make sense. If you want to assign values from one table to another you simply assign them. You don't traverse over the entire table until you find the right key to assign.

Aside from your isTable condition which seems to be always false you can replace that for loop by

Data.isQuestion = Object.isQuestion and Object.isQuestion or Data.isQuestion
Data.isId = Object.isId and Object.isId or Data.isId

Because you simply assign those values if they exist in Object.

Then there is this section which I cannot make sense of as I don't see how child will ever be a table:

if ChildIsTable then
  local FoundItem = FindQuestionInfo(child)
  if FoundItem then
   return FoundItem
  end
end

Also FindQuestionInfo(child) always returns Data so the condition

if FoundItem then
  return FoundItem
end

is not necessary.

So unless your Object will have a table inside that you didn't show in your example I don't see any reason to have this code at all. Especially not the recursive part.

You only copy parts of Object into a new table Data.

I cannot make sense of your problem description either.

I'm assuming you're asking about a xy-problem here. So I suggest you ask a new question about the actual problem you're trying to solve rather than about how to fix this code.

Upvotes: 1

Related Questions