require ("xxx") problem

Posted by Tkl1129 on Tue 10 Jan 2012 07:13 AM — 5 posts, 19,667 views.

Hong Kong #0
While I'm writing a code...
The case I have to access 2 .lua files..

1. db_map.lua (Contain the Map data in Lua table format)
2. f_path.lua (Contain the function code for path function)

normally when I write a code, I will start with require


function path_gen(a,b)
require("db_map")

local x = a
local y = b
local x1 = string.gsub(x,"%d+","")
local y1 = string.gsub(y,"%d+","")

local p_back = Map[x1][x].Back
local p_a1 = Map[x1][x].Path
local p_a2 = Map[y1][y].Path
local p_go = Map[y1][y].Go

print(p_back)
print(p_a1)
print(p_a2)
print(p_go)

end


This works...but what if I want to add other function into this code , the code was store inside the "f_path.lua"

When I add

function path_gen(a,b)
require("db_map")
require("f_path")

local x = a
local y = b
local x1 = string.gsub(x,"%d+","")
local y1 = string.gsub(y,"%d+","")

.......
............
local z = path_mix(x,y)



The function "path_mix" was store inside f_path, when I run this code, it occur error said f_path.lua not found...

so strange, cannot run 2 x lua file at the same time?
how it solve or write code in otherway? Thanks.
Australia Forum Administrator #1
Where are these two .lua files stored? In the same place as each other? What is that place?
Hong Kong #2
inside the lua folder

when I run "db_map"...inside are Lua table data...
it's work

or I seperate run the "f_path"...inside are functions...
it's work too

but when I run


require("db_map")
require("f_path")


the code when run the function which inside "f_path", it return "nil"....

so wanna ask is that not appropriate to run 2 lua together? or have another method like "module"
USA Global Moderator #3
I do multiple requires all the time. Does one of your required files change the working directory?
Hong Kong #4
so strange,
maybe I try to do more testing first.

By the way, do all functions keep in *.lua good?
or maybe use "module" function is more easy to manage?

just want to share and learn you guys's preference..thanks.