List of Enabled Triggers Group

Posted by Solara on Wed 19 Jul 2023 07:33 PM — 4 posts, 10,200 views.

USA #0
I'm trying to get a list of enabled trigger GROUPs.
I have the following, but it shows ALL enabled individual triggers, and the Group name. So the list will show multiple instances of the same Group name. Is there an easy way to parse/sort the table to remove duplicates?


triggerlist = GetTriggerList()
activetrigger = {}
if triggerlist then
  for k, v in ipairs (triggerlist ) do 
  if GetTriggerInfo (tostring (v), 8) == true then
  table.insert (activetrigger, tostring (GetTriggerInfo (v,26)))
  else
  end
end
end
  for k, v in ipairs (activetrigger ) do 
  table.sort (activetrigger)
  print (k,v)
end
Amended on Wed 19 Jul 2023 10:01 PM by Solara
USA Global Moderator #1
You can get just the list of unique groups if instead of doing

table.insert (activetrigger, tostring (GetTriggerInfo (v,26)))


you do


activetrigger[GetTriggerInfo(v,26) or ""] = true

and then instead of

for k, v in ipairs(activetrigger) do 
  print (k, v)
end

you do

for group, _ in pairs(activetrigger) do 
  print(group)
end


This treats the group name as the key in the table instead of as the value assigned to some meaningless integer key that you then need to deduplicate. Storing the group name as the key will guarantee that each group only gets stored once. But then you need to use pairs and not ipairs to loop over the list.
Amended on Wed 19 Jul 2023 09:10 PM by Fiendish
USA #2
Wow, I'll have to study that and figure out what was done. But it works great. Thanks!

Here's my final alias to get the list of enabled triggers by Group name, in alphabetical order:
require "pairsbykeys"
triggerlist = GetTriggerList()
activetrigger = {}
if triggerlist then
  for k, v in ipairs (triggerlist ) do 
  if GetTriggerInfo (tostring (v), 8) == true then
  activetrigger[GetTriggerInfo(v,26) or ""] = true
  else
  end
end
end
  for group, _ in pairsByKeys (activetrigger ) do 
  print (group)
end
Amended on Wed 19 Jul 2023 10:01 PM by Solara
USA Global Moderator #3
You don't need the empty else clause. You can just do if/end without else.

Here is where I tell you based on decades of experience that you really should pay attention to keeping strict indentation and line spacing patterns.

Without any content modifications, your code should be formatted more like


require "pairsbykeys"

triggerlist = GetTriggerList()
activetrigger = {}

if triggerlist then
  for k, v in ipairs(triggerlist) do
    if GetTriggerInfo(tostring(v), 8) == true then
      activetrigger[GetTriggerInfo(v, 26) or ""] = true
    else
    end
  end
end

for group, _ in pairsByKeys(activetrigger) do 
  print(group)
end


The Lua engine lets you get away with whitespace chaos, but human eyes and brains do not. Making your indentation match the contents makes reading and understanding the code later require a lot less mental energy.