ipairs!

Post Reply
Akimoto
Posts: 30
Joined: Sun May 08, 2011 5:54 pm

ipairs!

Post by Akimoto »

Code: [show] | [select all] lua
	for i, v in ipairs (ksys.myAffs.herb) do
		if (v == name) then
			
		table.remove(ksys.myAffs.herb, i)
			break
		end
	end
This code seems to remove the item from the table IF the items number is not a double or triple digit number, what am I doing wrong?

User avatar
keneanung
Site Admin
Posts: 94
Joined: Mon Mar 21, 2011 9:36 am
Discord: keneanung#2803

Re: ipairs!

Post by keneanung »

ipairs traverses a table that is coninously numbered and has no "holes" in it. If the next number is not found as an index, ipairs stops.

Akimoto
Posts: 30
Joined: Sun May 08, 2011 5:54 pm

Re: ipairs!

Post by Akimoto »

how would I do this with a table that there are many holes in it?
when I add the affliction:
table {
'all': table {
1: 'stupidity'
}
'herb': table {
22: 'stupidity'
}
'breaks': table {}
'writhe': table {}
'focus': table {
1: 'stupidity'
}
'special': table {}
'salve': table {}
'pipe': table {}
}

when I try to remove the affliction
table {
'all': table {}
'herb': table {
22: 'stupidity'
}
'breaks': table {}
'writhe': table {}
'focus': table {}
'special': table {}
'salve': table {}
'pipe': table {}
}

User avatar
chris
Posts: 493
Joined: Fri Jun 17, 2011 5:39 am

Re: ipairs!

Post by chris »

use pairs instead?

Akimoto
Posts: 30
Joined: Sun May 08, 2011 5:54 pm

Re: ipairs!

Post by Akimoto »

actually did this
Code: [show] | [select all] lua
function keyremove(table, key)
    local element = table[key]
    table[key] = nil
    return element
end

Post Reply