Welcome, Guest
Please Login or Register.    Lost Password?

Matching table result to a second table
(1 viewing) (1) Guest
Go to bottomPage: 12345
TOPIC: Matching table result to a second table
#20802
Matching table result to a second table 7 Months, 1 Week ago  
I have an idea for a new movie widget. It's based on the photo widget but instead of showing one picture at a time it would show a 5x4 grid of pictures. When you click the picture inside the box it would open the movie directly (the same as if you double click the file from windows explorer).

The reason for this widget is so my 5 year old niece can select a movie from my library without needing the ability to read the file name (over 200 movies).

The problem is that I don't know lua script and I'm just guessing.

First, how do I find out if a table entry exists in a second table?

Table A is created from a folder that consists only of movies but with different extensions.
Example Table A:
Finding Nemo.avi
Ice Age.flv
Shrek.mp4
The Little Mermaid.mpg
The Wizard of Oz.mkv

Table B is created from a folder that consists only of pictures but with different extensions.
Example Table B:
Finding Nemo.bmp
Shrek.jpg
The Little Mermaid.gif
The Wizard of Oz.png
Picture Not Found.jpg

If my result from table A is Shrek.mp4, how do I write the script code to find out if it is listed in table B and then extract the correct file name which should be Shrek.jpg? (If the file is not there - example: Ice Age.flv - it should default to Picture Not Found.jpg).

I will then have 2 urls, one for the picture preview and one to execute the movie. This will be done for every entry in table A.

I have been googling Lua manuals for days now but I cannot seem to understand how to apply what I read to my needs. Any help would be appreciated.
SereneSavage
Junior Boarder
Posts: 28
graphgraph
User Offline Click here to see the profile of this user
Gender: Female Location: Canada
The administrator has disabled public write access.
 
#20803
Re: Matching table result to a second table 7 Months, 1 Week ago  
Sort both tables without extension, then use merge algorithm:
Code:

A[1] < B[1] then table.insert(B,1,Picture Not Found); go next
A[1] > B[1] then table.remove(B,1); test again -- Unrelated image found
A[1] == B[1] then do nothing and go next -- Match


When that pseudocode finish parsing yours tables, You end with 2 tables of the same length, where elements of table A has a corresponding element on table B with the same index.
Nilfred
Junior Boarder
Posts: 23
graphgraph
User Offline Click here to see the profile of this user
Gender: Male Location: Argentina
Last Edit: 2014/03/17 20:47 By Nilfred.Reason: Typo. Reverse logic.
The administrator has disabled public write access.
 
#20804
Re:Matching table result to a second table 7 Months, 1 Week ago  
SereneSavage wrote:
I have an idea for a new movie widget.

Table A is created from a folder that consists only of movies but with different extensions.

Table B is created from a folder that consists only of pictures but with different extensions.

I have been googling Lua manuals for days now but I cannot seem to understand how to apply what I read to my needs. Any help would be appreciated.


This is a quick code I made. Instead of having 2 tables, you can have only 1, but with an internal structure. I'm using a Windows-like format. For UNIX-like format, using lower (lowercase) will help to avoid the case-sensitive.

Code:

local movie1 = {["title"]="Shrek", ["movExt"]="mp4", ["imgExt"]="jpg"}
local movie2 = {["title"]="Finding Nemo", ["movExt"]="avi", ["imgExt"]="bmp"}
local movie3 = {["title"]="Ice Age", ["movExt"]="flv", ["imgExt"]=""}
local movie4 = {["title"]="The Little Mermaid", ["movExt"]="mpg", ["imgExt"]="gif"}
local movie5 = {["title"]="The Wizard of Oz", ["movExt"]="mkv", ["imgExt"]="png"}
local movieTable = {movie1, movie2, movie3, movie4, movie5}
local moviePath = "C:\\Users\\UserName\\My Documents\\MovieWidget\\Movies\\"
local imagePath = "C:\\Users\\UserName\\My Documents\\MovieWidget\\Pictures\\"


function Movies_ListAllMoviesToLog()
  for i=1,#movieTable do
    Rainlendar_Log("----- MOVIE WIDGET" .. i .. ": " .. movieTable[i].title)  
  end
end

function Movies_SearchForMovie(title)
  for i=1,#movieTable do
    if (movieTable[i].title:lower() == title:lower()) then
      Rainlendar_Log("----- MOVIE PATH: " .. moviePath .. movieTable[i].title .. "." .. movieTable[i].movExt)
      if (movieTable[i].imgExt == "") then
        Rainlendar_Log("----- IMAGE PATH: " .. imagePath .. "Picture Not Found.jpg")
      else
        Rainlendar_Log("----- IMAGE PATH: " .. imagePath .. movieTable[i].title .. "." .. movieTable[i].imgExt)
      end
    end
  end
end

Movies_ListAllMoviesToLog()
Movies_SearchForMovie("sHrEk")
Movies_SearchForMovie("iCe AGE")

Jorge_Luis
Platinum Boarder
Posts: 795
graphgraph
User Offline Click here to see the profile of this user
Gender: Male My Rainlendar Skins Location: Argentina Birthday: 03/15
The administrator has disabled public write access.
 
#20807
Re:Matching table result to a second table 7 Months, 1 Week ago  
Here You are real code:
Code:

--
-- Merge algorithm explained
--

-- For debug purposes insert/remove -- below:
--[[
local function Rainlendar_Log( line )
  print(line)
end
--]]

local A = {
"Finding Nemo.avi", 
"Ice Age.flv", 
"Shrek.mp4", 
"The Little Mermaid.mpg", 
"The Wizard of Oz.mkv",
"Zorro XXX.avi" -- Make A larger than B to explore a (squashed) bug
}

local B = {
"Finding Nemo.bmp", 
"Shrek.jpg", 
"The Little Mermaid.gif", 
"The Wizard of Oz.png", 
"Picture Not Found.jpg"
}

--
-- Split Linux and Windows paths
-- Return: folder, filename without extension, extension with dot
--
local function Movies_SplitPath(path)
  return string.match(path, "(.-)([^\\/]-)(%.?[^%.\\/]*)$")
end

local path, file, extension
for i = 1, #A do
  path, file, extension = Movies_SplitPath(A[i])
  A[i] = { path, file, extension }
end

for i = 1, #B do
  path, file, extension = Movies_SplitPath(B[i])
  B[i] = { path, file, extension }
end

table.sort (A, function(a,b) return a[2]<b[2] end)
table.sort (B, function(a,b) return a[2]<b[2] end)
--
-- Sort then merge
--
for i = 1, #A do
  if i > #B or A[i][2] < B[i][2] then
    table.insert(B,i,{"", "Picture Not Found", ".jpg"})
  else
    while A[i][2] > B[i][2] do table.remove(B,i) end -- Unrelated image found 
  end
  Rainlendar_Log(table.concat(A[i]) .. " = " .. table.concat(B[i]))
end


Result:
Code:

Finding Nemo.avi = Finding Nemo.bmp
Ice Age.flv = Picture Not Found.jpg
Shrek.mp4 = Shrek.jpg
The Little Mermaid.mpg = The Little Mermaid.gif
The Wizard of Oz.mkv = The Wizard of Oz.png
Zorro XXX.avi = Picture Not Found.jpg


Looking at Jorge_Luis code, I realize that merge isn't necessary at all if your final objective is to search within the table. Tables are hashes in Lua. I will try that later.
Now, I should have provided some public functions as Jorge_Luis do
Nilfred
Junior Boarder
Posts: 23
graphgraph
User Offline Click here to see the profile of this user
Gender: Male Location: Argentina
Last Edit: 2014/03/17 20:52 By Nilfred.Reason: Move a dot around
The administrator has disabled public write access.
 
#20819
Re:Matching table result to a second table 7 Months, 1 Week ago  
Thank you for your quick responses and apologies for my late one.

Nilfred on your first post you simplified for me alot of what I was reading on google and trying to understand. The lack of code actually gave me ideas while at the same time giving me structure. I explored several different ways of attempting to achieve my goals and learned quite a bit.

Jorge_Luis although your code would run perfectly for the example movies, I would not want to rewrite the lua script each time I added a new movie to my folder, but thank you anyway. The code you supplied showed me the proper syntax and taught me to pay attention to which brackets to use.

Nilfred your second post made my head explode.
"return string.match(path, "(.-)([^\\/]-)(%.?[^%.\\/]*)$")"
What?!? Really?
I think table merging/reordering is a little over my head for now.

Update: I did manage to achieve what I wanted, although its rather sloppy. Just the fact that it works thrills me to no end. I wrote:

folder = Rainlendar_GetVariable(skin, window, "Movie_folder")
if folder ~= "" then
for filename in dirtree(folder) do
if string.find(filename:lower(), "(.*)") then
filename = string.gsub(filename, "\\", "/")
table.insert(g_Shadow4_Movie_Movies, filename)
position = string.len(folder)
filename = string.sub(filename, position + 2, -5)
table.insert(g_Shadow4_Movie_MovieName, filename)
end
end
end

This gave me Table Movies with the full path, filename and extension to use as the url for activating the movie and
Table Name with just the filename, no path or extension to use for comparison with the picture table. Next is:
menu = Rainlendar_GetVariable(skin, window, "Movie_menu")
if menu ~= "" then
for filename in dirtree(menu) do
if string.find(filename:lower(), "(.*)") then
filename = string.gsub(filename, "\\", "/")
table.insert(g_Shadow4_Movie_Pic, filename)
position = string.len(menu)
filename = string.sub(filename, position + 2, -5)
table.insert(g_Shadow4_Movie_PicName, filename)
end
end
end

This gave me the pictures to use for the preview and comparison. Then is:
m = 1
p = 1
repeat
repeat
if g_Shadow4_Movie_Name[m] == g_Shadow4_Movie_PicName[p] then
table.insert(g_Shadow4_Movie_Link, g_Savage_Movie_Pic[p])
m = m + 1
p = 1
break
else p = p + 1
end
until p >= #g_Shadow4_Movie_Pic
if p >= #g_Shadow4_Movie_Pic then
table.insert(g_Shadow4_Movie_Link, "None")
p = 1
m = m + 1
end
until m >= #g_Shadow4_Movie_Name

This gives me the link to the picture preview.

It only took me 5 tables, 4 days and 6 aspirin to achieve it.
SereneSavage
Junior Boarder
Posts: 28
graphgraph
User Offline Click here to see the profile of this user
Gender: Female Location: Canada
Last Edit: 2014/03/18 11:32 By SereneSavage.Reason: code looked ugly and squashed
The administrator has disabled public write access.
 
#20831
Re:Matching table result to a second table 7 Months, 1 Week ago  
I'm trying to run SereneSavage code outside Rainlendar:
Code:

-- For debug purposes insert/remove - below:
---[[
require "lfs"

function dirtree(dir)
  assert(dir and dir ~= "", "directory parameter is missing or empty")
  if string.sub(dir, -1) == "/" then
    dir=string.sub(dir, 1, -2)
  end

  local function yieldtree(dir)
    for entry in lfs.dir(dir) do
      if entry ~= "." and entry ~= ".." then
        entry=dir.."/"..entry
local attr=lfs.attributes(entry)
coroutine.yield(entry,attr)
if attr.mode == "directory" then
  yieldtree(entry)
end
      end
    end
  end

  return coroutine.wrap(function() yieldtree(dir) end)
end

local function Rainlendar_Log( line )
  print(line)
end

local function Rainlendar_GetVariable(skin, window, var)
  local vars = { ["Movie_folder"] = "", ["Movie_menu"] = "" }
  return vars[var]
end

local g_Shadow4_Movie_Movies = {}
local g_Shadow4_Movie_MovieName = {}
--]]

folder = Rainlendar_GetVariable(skin, window, "Movie_folder")
if folder ~= "" then
  for filename in dirtree(folder) do
    if string.find(filename:lower(), "(.*)") then
      filename = string.gsub(filename, "\\", "/")
      table.insert(g_Shadow4_Movie_Movies, filename)
      position = string.len(folder)
      filename = string.sub(filename, position + 2, -5)
      table.insert(g_Shadow4_Movie_MovieName, filename)
    end
  end
end
-- This gave me Table Movies with the full path, filename and extension to use as the url for activating the movie and
-- Table Name with just the filename, no path or extension to use for comparison with the picture table. Next is:
menu = Rainlendar_GetVariable(skin, window, "Movie_menu")
if menu ~= "" then
  for filename in dirtree(menu) do
    if string.find(filename:lower(), "(.*)") then
      filename = string.gsub(filename, "\\", "/")
      table.insert(g_Shadow4_Movie_Pic, filename)
      position = string.len(menu)
      filename = string.sub(filename, position + 2, -5)
      table.insert(g_Shadow4_Movie_PicName, filename)
    end
  end
end
-- This gave me the pictures to use for the preview and comparison. Then is:
m = 1
p = 1
repeat
  repeat
    if g_Shadow4_Movie_Name[m] == g_Shadow4_Movie_PicName[p] then
      table.insert(g_Shadow4_Movie_Link, g_Savage_Movie_Pic[p])
      m = m + 1
      p = 1
      break
    else p = p + 1
    end
  until p >= #g_Shadow4_Movie_Pic
    if p >= #g_Shadow4_Movie_Pic then
      table.insert(g_Shadow4_Movie_Link, "None")
      p = 1
      m = m + 1
    end
until m >= #g_Shadow4_Movie_Name
-- This gives me the link to the picture preview.


How function dirtree() is defined? Or where it is defined?
I put mine with unexpected result.

BTW feeding it with empty Movie_folder and Movie_menu variables result in a bug.
Nilfred
Junior Boarder
Posts: 23
graphgraph
User Offline Click here to see the profile of this user
Gender: Male Location: Argentina
Last Edit: 2014/03/18 20:27 By Nilfred.
The administrator has disabled public write access.
 
Go to topPage: 12345
get the latest posts directly to your desktop