Attract-Mode Support Forum

Attract-Mode Support => Scripting => Topic started by: iOtero on August 01, 2018, 08:19:57 AM

Title: Filter when filling an array (Solved)
Post by: iOtero on August 01, 2018, 08:19:57 AM
This code filled games_db array with ALL the roms in the rom path:

Code: [Select]
// games filter
local dir = DirectoryListing(system_rom_path);

local games_db = [];

// FILTER i do not know how to apply
local sel_game = "GUN";

foreach (dir_name in dir.results)
{
// strip
local f = txt.loadFile(dir_name);
local aa = [];
local i = 1;
foreach(line in f.lines)
{
// track
local a = split(line, "\\"); // split path-file for "/"
local title = strip(a.pop().slice(0, -4)); // only filename, without ext
local folder = strip(a.pop());            // path folder

aa.push(
{
"filename" : line,
"folder" : folder,
"title" : title,
  });
   i++;
}

games_db.push(aa);
}


What I need is to just fill in games_db with the rom that contain the string "GUN" in their filename.

Any suggestions?
Title: Re: Filter when filling an array
Post by: qqplayer on August 02, 2018, 04:59:15 AM
I think you can just create a filter

Code: [Select]
   filter               "Gun Games"
      rule                 Title contains gun

https://github.com/mickelson/attract/wiki/Example-filters
Title: Re: Filter when filling an array
Post by: iOtero on August 03, 2018, 04:45:29 AM
Thanks, but your idea can not be applied.
It is not the Attract-Mode database.
It's about reading the contents of a folder and filtering that data when putting them in an array: games.db  ;D
Title: Re: Filter when filling an array
Post by: iOtero on August 10, 2018, 01:27:03 PM
Luckily i alone have found the solution, otherwise...

Code: [Select]
local dir = DirectoryListing(system_rom_path);

foreach(dir_name in dir.results)
{
if(dir_name.find(sel_game)!= null)
{

// strip m3u
local f = txt.loadFile(dir_name);
local aa = [];
local i = 1;

foreach(line in f.lines)
{
// track
local l = split(line, "\\"); // split path-file for "/"
local name = strip(l.pop().slice(0, -4)); // only filename, without ext
local folder = strip(l.pop());            // path folder
local f = split(folder, "-");
local n = split(name, "-");
local album = strip(f[1]);
local artist = strip(n[1]);
local title = strip(n[2]);

aa.push(
{
"filename" : line,
"folder" : folder,
"title" : title,
"album" : album,
"artist" : artist
});
i++;
}
music_db.push(aa);
}
}