[Solved] "Unmodifiable" table gets wiped after using it

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

[Solved] "Unmodifiable" table gets wiped after using it

Post by Eglaios »

My problem is about a small code I wrote to take a table, then copy its content in a randomized order into another table ({1,2,3,4} -> {3,2,4,1}) :

I want to take the table "choices", shuffle it into "shuffled", then print both "choices" and "shuffled" contents (see code). I need to keep "choices" unmodified, to reuse it later.
When I run that, it first prints "1234" as "choices", and " " as "shuffled". However, when I run the shuffle(), it prints "shuffled" successfuly randomized, but then "choices" is printed as " ", so I guess it got wiped somehow in the process.
Then when I run shuffle() again, as "choices" is now empty, "shuffled" gets cleared too.

I don't understand how "choices" gets modified since all I do with it is setting its default value and printing it. I even tried to store it into "choices2" and call this new one for randomization, but even there, "choices" keeps getting wiped.

Furthermore, if I write "#tab - 1" instead of "#tab" in shuffle() 's For arguments, it only brings 3 numbers out of 4 to "shuffled", and the last number remains. When I do so, "choices" end up with this last number, which means "choices" somehow gets modified through shuffle(), even though it's not supposed to... any clue on why this happens?

Code: Select all

local shuffled = {}
local choices = {1,2,3,4}
local choices2 = choices

function love.load()
 love.window.setMode(1200, 780, {})
end

function love.keypressed(key)
 if key == "q" then
  shuffled = shuffle(choices2)
 end
end

function shuffle(tab)
local result = {}
 for inc=1, #tab, 1
 do
 rand = love.math.random(#tab)
 table.insert(result, tab[rand])
 table.remove(tab, rand)
 end
 return result
end

function love.draw()
 love.graphics.print(choices,0,0)
 love.graphics.print(shuffled,0,30)
end
-I'm not very experienced in coding yet, sorry if it may seem ugly
Last edited by Eglaios on Tue May 04, 2021 6:26 pm, edited 1 time in total.
Currently working on game music...
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: "Unmodifiable" table gets wiped after using it

Post by GVovkiv »

Code: Select all

local shuffled = {}
local choices = {1,2,3,4}

local function shuffle(tab)
  local result = {}
    for _ = 1, #tab do
    local rand = love.math.random(#tab)
    table.insert(result, tab[rand])
    end
  return result
end

function love.keypressed(key)
   if key == "q" then
    shuffled = shuffle(choices)
   end
end

function love.draw()
   love.graphics.print(choices,0,0)
   love.graphics.print(shuffled,0,20)
end
It what you expecting?

Also, i guess your "shuffle()" not actually shuffle given table, it's just generate new one where it can generate something like "1, 1, 1, 3", "2, 3, 4, 4", so, i think, you need table.sort function (https://www.lua.org/pil/19.3.html)
Last edited by GVovkiv on Tue May 04, 2021 4:18 pm, edited 1 time in total.
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: "Unmodifiable" table gets wiped after using it

Post by darkfrei »

Eglaios wrote: Tue May 04, 2021 2:59 pm I need to keep "choices" unmodified, to reuse it later.

Code: Select all

local choices = {1,2,3,4}
local choices2 = choices -- linked copy
You are need a make a copy, not linked copy:

Code: Select all

local choices = {1,2,3,4}
local choices2 = {} -- new table, not linked to the "choices"
for i, v in pairs (choices) do
	choices2[i]=v
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: "Unmodifiable" table gets wiped after using it

Post by GVovkiv »

darkfrei wrote: Tue May 04, 2021 4:15 pm
Eglaios wrote: Tue May 04, 2021 2:59 pm I need to keep "choices" unmodified, to reuse it later.

Code: Select all

local choices = {1,2,3,4}
local choices2 = choices -- linked copy
You are need a make a copy, not linked copy:

Code: Select all

local choices = {1,2,3,4}
local choices2 = {} -- new table, not linked to the "choices"
for i, v in pairs (choices) do
	choices2[i]=v
end
But...

Code: Select all

table = {1, 2, 3}
table2 = table
Isn't it just copy content from table, without linking?
Uh, nevermind, it really just linked copy, my bad
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

Re: "Unmodifiable" table gets wiped after using it

Post by Eglaios »

Searched a bit about it and found something better that works properly :

Code: Select all

function shuffle(tbl)
  for i = #tbl, 2, -1 do
    local j = math.random(i)
    tbl[i], tbl[j] = tbl[j], tbl[i]
  end
  return tbl
end
It simply shuffles the targetted table, good enough for me.

Though thank you all for your answers! I didn't know about the "choices = choices2" which didn't work, nice for later. I just got into tables functions and didn't find yet about table.sort tho, I will sure look at that.
Kinda learning what I need as I get further, but it wouldn't be a bad idea to read about the whole thing first, that could avoid forum spamming by myself :ehem:
Currently working on game music...
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: [Solved] "Unmodifiable" table gets wiped after using it

Post by pgimeno »

Copy and randomize at the same time:

Code: Select all

local function CopyRandomize(t) -- tribute to Spectrum 128K
  local res = {}
  for i = 1, #t do
    local j = math.random(1, i)
    res[i] = res[j]
    res[j] = t[i]
  end
  return res
end
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: "Unmodifiable" table gets wiped after using it

Post by darkfrei »

GVovkiv wrote: Tue May 04, 2021 4:22 pm Isn't it just copy content from table, without linking?
Uh, nevermind, it really just linked copy, my bad
And this was mine: https://love2d.org/forums/viewtopic.php ... 95#p238395
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests