Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Need little lua script to alphabetize my drop down lists

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General Gamehacking
View previous topic :: View next topic  
Author Message
Drivium
Advanced Cheater
Reputation: 0

Joined: 16 Apr 2013
Posts: 97

PostPosted: Wed Jul 03, 2019 10:17 am    Post subject: Need little lua script to alphabetize my drop down lists Reply with quote

I've got several drop down lists (hundreds) and when I initially designed it, I alphabetized the items by the string name as opposed to the friendly name. It's making finding the drop down item I want quite difficult. Hoping this can be solved with LUA.

Would just be a 1 time use thing... It would run through my drop down lists and alphabetize each. Then I'll save my table and be good to go. Smile

Hoping it can alphabetize by the friendly names as opposed to the string.

In other words, alphabetize by the items on the right side of :

Weapon_Bow_015:Golden Bow
Weapon_Bow_016:Swallow Bow
Weapon_Bow_017:Falcon Bow
Weapon_Bow_023:Ancient Bow
Weapon_Bow_026:Mighty Lynel Bow
Weapon_Bow_027:Dragon Bone Boko Bow
Weapon_Sword_048:Meteor Rod
Weapon_Sword_049:Blizzard Rod
Weapon_Sword_050:Thunderstorm Rod
Weapon_Sword_051:Boomerang

Thanks in advance!
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Thu Jul 04, 2019 2:36 pm    Post subject: Reply with quote

I played around with this
Code:
print('\n\n\n\n\n') -- make new output obvious when testing

-- input in one new line separated string
local str = [[Weapon_Sword_048:Meteor Rod
Weapon_Sword_049:Blizzard Rod
Armor_Helm_999:Some Test Armor
Weapon_Sword_050:Thunderstorm Rod
Weapon_Sword_051:Boomerang
Weapon_Bow_015:Golden Bow
Weapon_Bow_016:Swallow Bow
Weapon_Bow_017:Falcon Bow
Armor_Plate_999:Some Other Armor
Armor_Plate_999:Some Heavier Armor
Weapon_Bow_023:Ancient Bow
Weapon_Bow_026:Mighty Lynel Bow
Weapon_Bow_027:Dragon Bone Boko Bow]]

local list = {} -- for sorting

-- split lines, [^...]=not in this set, \n=new line, +=1 or more
for line in str:gmatch('[^\n]+') do
  -- split info d=digits, (...)=separate group/capture
  for class,type,id,name in line:gmatch('([^_]+)_([^_]+)_(%d+):([^\n]+)') do
    -- put into table for sorting later
    table.insert(list, {line, class, type, name, id})
  end
end

table.sort(list, function(a, b)
  if a[2] ~= b[2] then return a[2] < b[2] -- weapon/armor
  elseif a[3] ~= b[3] then return a[3] < b[3] -- bow/sword
  elseif a[4] ~= b[4] then return a[4] < b[4] -- name
  else return a[5] < b[5] end -- number
end)

-- print sorted list, alternatively clear and add in order to something else
for k,v in ipairs(list) do
  -- %-40s = print string, left aligned, with at least 40 characters
  print(('%-40s %s %s "%s"'):format(v[1],v[2], v[3], v[4]))
end


maybe you can find something useful in it.

_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 199

Joined: 25 Jan 2006
Posts: 8518
Location: 127.0.0.1

PostPosted: Thu Jul 04, 2019 7:32 pm    Post subject: Reply with quote

You can split the strings as they are being sorted and compare the ending part like this:

Code:

local items =
{
  'Weapon_Bow_015:Golden Bow',
  'Weapon_Bow_016:Swallow Bow',
  'Weapon_Bow_017:Falcon Bow',
  'Weapon_Bow_023:Ancient Bow',
  'Weapon_Bow_026:Mighty Lynel Bow',
  'Weapon_Bow_027:Dragon Bone Boko Bow',
  'Weapon_Sword_048:Meteor Rod',
  'Weapon_Sword_049:Blizzard Rod',
  'Weapon_Sword_050:Thunderstorm Rod',
  'Weapon_Sword_051:Boomerang'
};

local function split(str, sep)
    local sep = sep or ':';
    local ret = {};
    str:gsub(string.format('([^%s]+)', sep), function (s)
        ret[#ret + 1] = s;
    end);
    return ret;
end

table.sort(items, function(a, b)
    local p1 = split(a, ':');
    local p2 = split(b, ':');
    return p1[2] < p2[2];
end);

for k, v in pairs(items) do
    print(v);
end


Which will result in:
Code:

Weapon_Bow_023:Ancient Bow
Weapon_Sword_049:Blizzard Rod
Weapon_Sword_051:Boomerang
Weapon_Bow_027:Dragon Bone Boko Bow
Weapon_Bow_017:Falcon Bow
Weapon_Bow_015:Golden Bow
Weapon_Sword_048:Meteor Rod
Weapon_Bow_026:Mighty Lynel Bow
Weapon_Bow_016:Swallow Bow
Weapon_Sword_050:Thunderstorm Rod

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Drivium
Advanced Cheater
Reputation: 0

Joined: 16 Apr 2013
Posts: 97

PostPosted: Fri Jul 05, 2019 11:00 am    Post subject: Reply with quote

Thank you both for the solutions! This will save me so much manual work!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General Gamehacking All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites