Skip to content

Check if the targets need conversion in NeuralLib - loop issues #8

@dcuny

Description

@dcuny

In line 272 of NeuralLib, you're looping through a table looking for a match. If a match is found, you remove the item from the table with a custom routine tabledel():

  -- Check if the targets need conversion
  for i=1,#labels do
    if labels[i] == target then
      target_to_num = true
      -- new labels table without the target
      labels = tabledel(labels,i)
    end
  end

The problem is that even though you removed the item, you continue looping through the table. The next item will actually be skipped (as it's been moved down to where the removed item is), and since the table is shorter, Lua will return a nil once it goes past the end of the table.

This won't cause an error, but it's not the proper behavior.

Since there's only a single item you're looking for, once you find a match, you can exit the loop.

Additionally, there's no need for writing your own tabledel function, as Lua's table.remove() will do the same. Since the table is passed by reference to table.remove() , the value doesn't have to be assigned back.

Finally, using ipairs to loop makes things a bit simpler:

  -- loop through the labels to see if target is in the table
  for i, label in ipairs( labels ) do
    if label == target then
      -- set the flag, remove target from the labels, and exit loop
      target_to_num = true
      table.remove(labels,i)
      break
    end
  end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions