Page 2 of 2
Re: How to save entire contents of a table to a text file?
Posted: Tue Oct 30, 2012 10:59 am
by Mario-Fan
Wojak wrote:this is working on my PC:
Code: Select all
if songFile and love.filesystem.exists(love.filesystem.getSaveDirectory( ).."/"..songFile) then
songMusic = love.audio.newSource(songFile, "stream")
end
I got it working, but I don't want it to loop. Can I have it open
and not loop?
Re: How to save entire contents of a table to a text file?
Posted: Tue Oct 30, 2012 11:57 am
by Wojak
You are talking about the sound?
If yes then:
https://love2d.org/wiki/Source
Re: How to save entire contents of a table to a text file?
Posted: Tue Oct 30, 2012 12:00 pm
by Mario-Fan
How could I have missed that?!
EDIT: I would like to know if there's a function that returns the length of a source. I couldn't find it on the Wiki.
Re: How to save entire contents of a table to a text file?
Posted: Tue Oct 30, 2012 2:16 pm
by Robin
What I don't get is why ending with a comma doesn't work. It should and does work on my machine.
Using
#t doesn't work here because
- you are comparing it to values, instead of indexes.
- you only have string keys, which means #t is always 0.
This is a better solution if you really don't want terminating commas:
Code: Select all
write_table = function (t)
local s = "{"
local first = true
for k,v in pairs(t) do
if first then
first = false
else
s = s .. ", "
end
s = s .. k .. " = " .. v
end
s = s .. "}"
return s
end
Then call
print(write_table({a=1, b=2, c=3})), it should print
{a=1, b=2, c=3}.
The order is arbitrary, that is because it is a hash table, which means keys are no stored in any order you would expect, but instead stored in a way that makes it fast to retrieve items from the table.
Re: How to save entire contents of a table to a text file?
Posted: Tue Oct 30, 2012 2:27 pm
by Mario-Fan
Robin wrote:What I don't get is why ending with a comma doesn't work. It should and does work on my machine.
Using
#t doesn't work here because
- you are comparing it to values, instead of indexes.
- you only have string keys, which means #t is always 0.
This is a better solution if you really don't want terminating commas:
Code: Select all
write_table = function (t)
local s = "{"
local first = true
for k,v in pairs(t) do
if first then
first = false
else
s = s .. ", "
end
s = s .. k .. " = " .. v
end
s = s .. "}"
return s
end
Then call
print(write_table({a=1, b=2, c=3})), it should print
{a=1, b=2, c=3}.
The order is arbitrary, that is because it is a hash table, which means keys are no stored in any order you would expect, but instead stored in a way that makes it fast to retrieve items from the table.
Thanks, I didn't realize, but I don't really think that I need the order to be right in this program. I replaced the write_table function though.