files_scripts icon indicating copy to clipboard operation
files_scripts copied to clipboard

unexpected loop behavior when using for

Open disgustipated opened this issue 1 year ago • 1 comments

perhaps this is my lack of knowledge of lua, but im having trouble getting reliable output as im working on a script with the end goal of applying tags to files. i have the following script outputting to log while im getting familiar

function applyTags(nodes, tags)
  log(" incoming tag ")
  log(json(tags))
  for i, node in ipairs(nodes) do
  	if( is_file(node) )then
      log("node is a file " .. i)
      log(json(node))
      --for t, workingtag in ipairs(tags) do
      --  log(json(workingtag[t]))
      --end
    elseif( is_folder(node) )then
      --loop through list var and apply tag to each file directory_listing(node)
    end
  end
end

applyTags(get_input_files(), get_input('PrintSize'))

that produces the log output like this, but doesnt list the second file image

and this

function applyTags(nodes, tags)
  log(" incoming tag ")
  log(json(tags))
  for i, node in ipairs(nodes) do
    log("before file check " .. i .. " " .. node.name)
  	if( is_file(node) )then
      log("node is a file " .. i .. " " .. node.name)
    elseif( is_folder(node) )then
      --loop through list var and apply tag to each file directory_listing(node)
    end
  end
end

applyTags(get_input_files(), get_input('PrintSize'))

produces the log output "before file check" but seems the is_file doesnt happen and the "node is file" log doesnt get output. im not sure if im troubleshooting log output or my lack of knowledge of lua and could use some help understanding whats going on

disgustipated avatar Nov 24 '24 13:11 disgustipated

Yeah, I'm not understanding why the nested loop doesnt hit the second item. With this code i get the listing outside fine but in the inside loop im only getting the first item.

function applyTags(nodes, tags)
  log(" incoming tag ")
  log(json(tags))
  for g,t in next, tags do
    log("listing tags outside" .. g .. " " .. t)
  end
  for i, node in ipairs(nodes) do
    log("before file check " .. i .. " " .. node.name)
    if( is_file(node) )then
      log("node is a file " .. i .. " " .. node.name)
      for g,t in next, tags do
        log("listing tags inside" .. g .. " " .. t)
      end
    elseif( is_folder(node) )then
      --loop through list var and apply tag to each file directory_listing(node)
    end
  end
end
applyTags(get_input_files(), get_input('PrintSize'))

image

yeah there is something that is going on with counting through the loop, if i have two files selected i get the listing tags inside twice as expected on the first file, but the second file only does the first item. i didnt see it at first because i was only selecting a single file while testing. image

disgustipated avatar Nov 25 '24 11:11 disgustipated