gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
How would I shuffle only the asterisked part of this list in Love2D, but make it work…
- User
How would I shuffle only the asterisked part of this list in Love2D, but make it work for any of this kind? example = {one=1 two=2}
- Assistant
To shuffle only the asterisked part of the list in Love2D, you can create a separate table for the asterisks and shuffle that table, while keeping the original table intact. Here's an example code:
-- the original table example = {one="*1*", two="*2*"} -- find all the asterisked values and put them in a separate table asterisks = {} for key, value in pairs(example) do if string.sub(value, 1, 1) == '*' and string.sub(value, -1) == '*' then table.insert(asterisks, value) end end -- shuffle the asterisks table math.randomseed(os.time()) for i = #asterisks, 2, -1 do local j = math.random(i) asterisks[i], asterisks[j] = asterisks[j], asterisks[i] end -- replace the asterisked values in the original table with the shuffled ones j = 1 for key, value in pairs(example) do if string.sub(value, 1, 1) == '*' and string.sub(value, -1) == '*' then example[key] = asterisks[j] j = j + 1 end end -- print the shuffled table for key, value in pairs(example) do print(key .. " = " .. value) endThis code uses the
string.subfunction to find the asterisked values and put them into theasteriskstable. It then shuffles that table using the Fisher-Yates algorithm. Finally, it replaces the original asterisked values with the shuffled ones. Note that this code assumes that there are no duplicate asterisked values in the original table.