1

I was working on a script to randomize the data inside of my array but I get and error that says

unexpected symbol near "#"

When I go to that line, and I remove the "#" I get

attempt to perform arithmetic on local `n' (a table value)

Here is my shuffle function

function shuffle(array)
    local array = array
    local n = #array
    local j
    local random = math.random
    for i=n-1, 1, -1 do
        j = random(i)
        array[j],array[i] = array[i],array[j]
    end
    return array
end

and here is what I am trying to randomize

shuffle(new_players)
for name,character in pairs(new_players) do
    if (character.inside == true and character.death == 0) then
        local player = getPlayerByName(name, map_copy)
        if (player ~= nil) then
            addState(player)
            break
        end
    end
end

Here is my array

new_players= { }
new_players[charName] = { death = 0, inside= true }

Any help? If i am doing something completely wrong?

8
  • 1
    You can't shuffle an associative array. – Egor Skriptunoff Jun 15 '13 at 7:30
  • 1
    Why break after addState? Do you need only one random player? – Egor Skriptunoff Jun 15 '13 at 7:32
  • 5
    Why do you have the line local array = array? – derkyjadex Jun 15 '13 at 8:44
  • 1
    Which version of Lua are using? – lhf Jun 15 '13 at 11:52
  • @EgorSkriptunoff Yes only need one random player. – Derick Jun 15 '13 at 16:30
3

1) Try change charName from string to a number.

2) For shuffle you can use this code:

function swap(array, index1, index2)
    array[index1], array[index2] = array[index2], array[index1]
end

function shuffle(array)
    local counter = #array
    while counter > 1 do
        local index = math.random(counter)
        swap(array, index, counter)
        counter = counter - 1
    end
end
2
  • Do I use this after I change charName from string to number? Or does not matter? – Derick Jun 15 '13 at 16:57
  • As you wish. In your code, there are strange things, like local array = array, which contribute to the code ambiguous behavior. Your and my functions are very similar, but my reads better. P.S. Sorry for Google-translating. – Dmi7ry Jun 16 '13 at 2:01
3

If your Lua version is < 5.1 then there is no # operator. Use table.getn instead:

local n = table.getn(array);

(Update) Note that your function, while it does shuffle the items around, it does not really shuffle all elements. Also since you reduce the range with each iteration, you will almost certainly swap the first 10% of your array around multiple times. Now swapping them multiple times is not bad by itself, but that you are, by comparison, almost never swapping the other elements is.

So one option to solve this would be to always use the same range for your random variable. And I would go even further and select two random indexes to swap:

function shuffle(array)
    local n, random, j = table.getn(array), math.random
    for i=1, n do
        j,k = random(n), random(n)
        array[j],array[k] = array[k],array[j]
    end
    return array
end

The other option would be to select random elements from the source array and put them into a new output array:

local rnd,trem,getn,ins = math.random,table.remove,table.getn,table.insert;
function shuffle(a)
    local r = {};
    while getn(a) > 0 do
        ins(r, trem(a, rnd(getn(a))));
    end
    return r;
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.