String format related question

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.
Post Reply
User avatar
Cryogenical
Prole
Posts: 49
Joined: Mon Apr 28, 2014 5:23 pm

String format related question

Post by Cryogenical » Tue Sep 16, 2014 5:39 pm

I've been trying to create a playlist with a list of songs.
These songs are of course sorted in a path format, so

Code: Select all

list = {
     "assets/music/matryoshkachorus.mp3",
     "assets/music/senbonzakurachorus.mp3",
     "assets/music/noushouchorus.mp3",
     "assets/music/headphoneactorchorus.mp3",
     "assets/music/nyanchorus.mp3",
     "assets/music/rimokonchorus.mp3",}
when I try to sort the table and add each song to a list, the names are "assets/music/matryoshkachorus.mp3" and so forth. I just want to sort the names and format out the path ("assets/music/") and the .mp3 suffix.
Been searching for a solution, and will keep searching.

User avatar
Cryogenical
Prole
Posts: 49
Joined: Mon Apr 28, 2014 5:23 pm

Re: String format related question

Post by Cryogenical » Tue Sep 16, 2014 5:52 pm

I cheated and made a separate list for titles. Still think there's a better way to go about this.

Code: Select all

namelist = {
     "Matryoshka Chorus",                       -- one
     "Senbonzakura Chorus",                     -- two
     "Noushou Chorus",                          -- three
     "HeadphoneActor Chorus",                   -- four
     "Nyan Chorus",                             -- five
     "Rimokon Chorus",                          -- six
     }

    songlist = {
     "assets/music/matryoshkachorus.mp3",       -- one
     "assets/music/senbonzakurachorus.mp3",     -- two
     "assets/music/noushouchorus.mp3",          -- three
     "assets/music/headphoneactorchorus.mp3",   -- four
     "assets/music/nyanchorus.mp3",             -- five
     "assets/music/rimokonchorus.mp3",          -- six
     }
Sorted it using

Code: Select all

for i,title in ipairs(namelist) do
            songchoice:AddChoice(title)
        end
EDIT: I'm using the APIs LoveFrames and TEsound.
Now when I try to play the songs using this multichoice list, it's not playing anything. I know it's because it's unable to find the path, but I don't know how to compensate for that.

User avatar
DaedalusYoung
Party member
Posts: 402
Joined: Sun Jul 14, 2013 8:04 pm

Re: String format related question

Post by DaedalusYoung » Tue Sep 16, 2014 6:14 pm

Without knowing the rest of your code; why not use the filename as table key and the song name as value?

Code: Select all

songlist = {
    matryoshkachorus = "Matryoshka Chorus",
    senbonzakurachorus = "Senbonzakura Chorus",
    -- and so on
    }
Then instead of ipairs, use pairs.

Note I only used the filename, I didn't add the "assets/music/" nor ".mp3", because if these things are the same for all songs, you don't need to add them, just add that in your song load function.

User avatar
Cryogenical
Prole
Posts: 49
Joined: Mon Apr 28, 2014 5:23 pm

Re: String format related question

Post by Cryogenical » Tue Sep 16, 2014 6:29 pm

There's no song load function, with TEsound you're supposed to be able to load a song by using it's "TEsound.play(songpath)" function. I've attempted what you just told me to do to no results.
I know that I'm doing something ridiculously wrong, but I just don't know what else to do.

I'm probably not utilizing these sorting functions correctly.
What's the difference between using ipairs and pairs?

User avatar
zorg
Party member
Posts: 2747
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: String format related question

Post by zorg » Wed Sep 17, 2014 7:40 am

Edit: Roland explained what i tried to better below me

DaedalusYoung probably meant this then:

Code: Select all

TEsound.play("assets/music/" .. songname[filename] .. ".mp3")
On the other hand,
when sorting, you could sort with using gsub and a pattern that returns only what's after the last /, or in other words, the filename and the extension, and then it will sort comparing those only, not the full path.


the difference between ipairs and pairs is that ipairs only goes through integer keys (1,2,3,...n) until the first gap, while pairs goes through everything in the table.
Last edited by zorg on Wed Sep 17, 2014 10:44 am, edited 3 times in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
Roland_Yonaba
Inner party member
Posts: 1562
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: String format related question

Post by Roland_Yonaba » Wed Sep 17, 2014 8:09 am

Hi,
Cryogenical wrote:What's the difference between using ipairs and pairs?
The difference lies in the way Lua treats tables: array-like or map-like. "Ipairs" will let you iterate on all values in the array part of a table, while "pairs" will go through all the values inside this table, being in the array part and the hash part.
I remeber I wrote recently a lengthy post on it, that you can find here. The part titled "Tables" is all relevant to you, I believe. And it was written in a straightforward manner, so I hope it helps.
For more details, you can take a look at "What's the difference between pairs and ipairs ?" on luafaq.

Now, concerning the initial issue, I think there are many ways to solve the problem. I can suggest one, though I am not advocating it is the best way. The Great DaedalusYoung cleverly mentionned it was not necessary to keep prefixes and suffixes for each entry in the songlist, since they were the same. I'll go with that, and treat then the songlist as a simple array:

Code: Select all

local songlist = { "song1", "song2", "song3",...}
In that respect, to sort all those songs in alphabetical order, i'll just write:

Code: Select all

table.sort(songlist)
You can also have the following to format properly a songname to a path:

Code: Select all

local function wrapString(str, prefix, suffix)
  return prefix..str..suffix
end

for _, songname in ipairs(songlist) do
  print(wrapString(songname, "assets/music/", ".mp3"))
end
Another way to handle it is to keep the original table as-is, i.e store the full song path instead of the name only, and sort elements, but using pattern matching tricks to extract the songnames only and compare them. For instance, this:

Code: Select all

table.sort(songlist, function (a, b) 
  local songnameA = a:match('.+/(.+)%.mp3$')
  local songnameB = b:match('.+/(.+)%.mp3$')
  return songnameA < songnameB
end)
Hope this helps!

Post Reply

Who is online

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