How to save entire contents of a table to a text file?

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.
Mario-Fan
Prole
Posts: 18
Joined: Wed Jun 06, 2012 6:37 pm

Re: How to save entire contents of a table to a text file?

Post 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?
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: How to save entire contents of a table to a text file?

Post by Wojak »

You are talking about the sound?
If yes then:

Code: Select all

songMusic:setLooping( false ) 
https://love2d.org/wiki/Source
Mario-Fan
Prole
Posts: 18
Joined: Wed Jun 06, 2012 6:37 pm

Re: How to save entire contents of a table to a text file?

Post by Mario-Fan »

Wojak wrote:You are talking about the sound?
If yes then:

Code: Select all

songMusic:setLooping( false ) 
https://love2d.org/wiki/Source
How could I have missed that?! :oops:
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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to save entire contents of a table to a text file?

Post 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
  1. you are comparing it to values, instead of indexes.
  2. 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.
Help us help you: attach a .love.
Mario-Fan
Prole
Posts: 18
Joined: Wed Jun 06, 2012 6:37 pm

Re: How to save entire contents of a table to a text file?

Post 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
  1. you are comparing it to values, instead of indexes.
  2. 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests