so that the table:
a[13] = "Just" a[42] = "an" a[500] = "example!"will become:
a[1] = "Just" a[2] = "an" a[3] = "example!"is there something built into lua to do this or do I have to right code for this?
Posted 28 April 2014 - 06:47 PM
a[13] = "Just" a[42] = "an" a[500] = "example!"will become:
a[1] = "Just" a[2] = "an" a[3] = "example!"is there something built into lua to do this or do I have to right code for this?
Posted 28 April 2014 - 07:27 PM
function table.condense(tab,rewrite)
--this function condenses a tables numerical keys into the lowest it can.
--note, if rewrite is false, it will just return the condensed form. It will not rewrite the table.
--initiation of variables
if type(rewrite) ~="boolean" then rewrite = true end
local condensing = {}
assert(type(tab) == "table",
"Bad item to 'condense'. (table expected, got "..type(tab)..")")
--part to get all the numeric pairs off.
for key,value in pairs(tab) do
if type(key)=="number" then
condensing[#condensing+1]=tab[key]
if rewrite then tab[key]=nil end
end
end
--part to stick them back in.
if rewrite then
for i=1,#condensing do
tab[i]=condensing[i]
end
else
return condensing
end
end
Edited by CCJJSax, 28 April 2014 - 07:40 PM.
Posted 28 April 2014 - 07:31 PM
CCJJSax, on 28 April 2014 - 07:27 PM, said:
Edited by Termanater13, 28 April 2014 - 07:34 PM.
Posted 28 April 2014 - 07:32 PM
Termanater13, on 28 April 2014 - 07:31 PM, said:
CCJJSax, on 28 April 2014 - 07:27 PM, said:
Posted 28 April 2014 - 07:37 PM
CCJJSax, on 28 April 2014 - 07:32 PM, said:
Termanater13, on 28 April 2014 - 07:31 PM, said:
Posted 28 April 2014 - 07:42 PM
function table.condense(tab, rewrite)
if type(rewrite) ~= "boolean" then rewrite = true end
local condensing = {}
assert(type(tab) == "table",
"Bad item to 'condense'. (table expected, got "..type(tab)..")")
for key, value in pairs(tab) do
if type(key) == "number" then
condensing[#condensing+1]=tab[key]
if rewrite then tab[key] = nil end
end
end
if rewrite then
for i = 1, #condensing do
tab[i] = condensing[i]
end
else
return condensing
end
end
Termanater13, on 28 April 2014 - 07:37 PM, said:
CCJJSax, on 28 April 2014 - 07:32 PM, said:
Termanater13, on 28 April 2014 - 07:31 PM, said:
Posted 28 April 2014 - 07:44 PM
Posted 28 April 2014 - 07:50 PM
CometWolf, on 28 April 2014 - 07:44 PM, said:
Posted 28 April 2014 - 07:56 PM
t = {
[2] = "derp",
[5] = "what",
[500] = "sortdat"
}
local compressed = {}
for k,v in pairs(t) do
compressed[#compressed+1] = {
key = k,
value = v
}
end
table.sort(
compressed,
function(v1,v2)
return v1.key < v2.key
end
)
local ordered = {}
for i=1,#compressed do
ordered[i] = compressed[i].value
end
Edited by CometWolf, 28 April 2014 - 08:00 PM.
Posted 28 April 2014 - 08:31 PM
CometWolf, on 28 April 2014 - 07:56 PM, said:
t = {
[2] = "derp",
[5] = "what",
[500] = "sortdat"
}
local compressed = {}
for k,v in pairs(t) do
compressed[#compressed+1] = {
key = k,
value = v
}
end
table.sort(
compressed,
function(v1,v2)
return v1.key < v2.key
end
)
local ordered = {}
for i=1,#compressed do
ordered[i] = compressed[i].value
end
Posted 28 April 2014 - 08:47 PM
Posted 28 April 2014 - 10:11 PM
CometWolf, on 28 April 2014 - 08:47 PM, said:
Posted 29 April 2014 - 12:20 AM
a[13] = "Just" a[42] = "an" a[500] = "example!"
local function squash(myTable) local curIndex = 1 for i=1,table.maxn(myTable) do if myTable[i] then myTable[curIndex] = myTable[i] myTable[i] = nil curIndex = curIndex + 1 end end end
Posted 29 April 2014 - 02:06 AM
Edited by HometownPotato, 29 April 2014 - 02:08 AM.
Posted 29 April 2014 - 02:58 AM
Posted 29 April 2014 - 03:00 AM
Posted 29 April 2014 - 03:10 AM
function compressSparse(tab)
local keySet = {}
for i in pairs(tab) do
table.insert(keySet, i)
end
table.sort(keySet)
local retVal = {}
for i = 1, #keySet do
retVal[i] = tab[keySet[i]]
end
return retVal
end
Posted 29 April 2014 - 03:16 AM
Posted 29 April 2014 - 03:23 AM
0 members, 1 guests, 0 anonymous users