Hi, Does anyone know how can we use a variable declared inside if statement outside if block? if sip_request_user == nil then sip_request_user = params:getHeader("sip_request_user") else local sip_request_user1 = tonumber(sip_request_user) end In my lua I have something like above and I want to use the variable sip_request_user1 outside if. What can be done? Regards, Archana
I'm assuming you need to remove the "local" directive there. -MC On Mon, Nov 26, 2012 at 8:29 AM, Archana Venugopan <a.venugopan@mundio.com>wrote:
Hi,****
** **
Does anyone know how can we use a variable declared inside if statement outside if block?****
** **
if sip_request_user == nil then****
sip_request_user = params:getHeader("sip_request_user")****
else****
local sip_request_user1 = tonumber(sip_request_user)****
end****
** **
In my lua I have something like above and I want to use the variable sip_request_user1 outside if. What can be done?****
** **
Regards,****
Archana****
** **
_________________________________________________________________________ Professional FreeSWITCH Consulting Services: consulting@freeswitch.org http://www.freeswitchsolutions.com
FreeSWITCH-powered IP PBX: The CudaTel Communication Server http://www.cudatel.com
Official FreeSWITCH Sites http://www.freeswitch.org http://wiki.freeswitch.org http://www.cluecon.com
FreeSWITCH-users mailing list FreeSWITCH-users@lists.freeswitch.org http://lists.freeswitch.org/mailman/listinfo/freeswitch-users UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users http://www.freeswitch.org
-- Michael S Collins Twitter: @mercutioviz http://www.FreeSWITCH.org http://www.ClueCon.com http://www.OSTAG.org
Remove the local keyword from the variable declaration on sip_request_user1. On Mon, Nov 26, 2012 at 11:29 AM, Archana Venugopan <a.venugopan@mundio.com>wrote:
Hi,****
** **
Does anyone know how can we use a variable declared inside if statement outside if block?****
** **
if sip_request_user == nil then****
sip_request_user = params:getHeader("sip_request_user")****
else****
local sip_request_user1 = tonumber(sip_request_user)****
end****
** **
In my lua I have something like above and I want to use the variable sip_request_user1 outside if. What can be done?****
** **
Regards,****
Archana****
** **
_________________________________________________________________________ Professional FreeSWITCH Consulting Services: consulting@freeswitch.org http://www.freeswitchsolutions.com
FreeSWITCH-powered IP PBX: The CudaTel Communication Server http://www.cudatel.com
Official FreeSWITCH Sites http://www.freeswitch.org http://wiki.freeswitch.org http://www.cluecon.com
FreeSWITCH-users mailing list FreeSWITCH-users@lists.freeswitch.org http://lists.freeswitch.org/mailman/listinfo/freeswitch-users UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users http://www.freeswitch.org
I would advise leaving the local keyword. Local variable lookups are faster in Lua, and more importantly it's best to restrict the variable's scope to the smallest scope you'll use it in. Even if you're using the variable at the scope of an entire script, you should declare it local to the script -- global variables are accessed beyond the actual script they are in, as illustrated by the following example: test1.lua: require "test2" print(string.format("foo in test1 is: %s", tostring(foo))) print(string.format("baz in test1 is: %s", tostring(baz))) test2.lua: foo = "bar" local baz = "bang" print(string.format("foo in test2 is: %s", tostring(foo))) print(string.format("baz in test2 is: %s", tostring(baz))) Output: foo in test2 is: bar baz in test2 is: bang foo in test1 is: bar baz in test1 is: nil Hope this helps. hunmonk On Monday, November 26, 2012 at 11:00 AM, Bryant Lee wrote:
Remove the local keyword from the variable declaration on sip_request_user1.
On Mon, Nov 26, 2012 at 11:29 AM, Archana Venugopan <a.venugopan@mundio.com (mailto:a.venugopan@mundio.com)> wrote:
Hi,
Does anyone know how can we use a variable declared inside if statement outside if block?
if sip_request_user == nil then sip_request_user = params:getHeader("sip_request_user") else local sip_request_user1 = tonumber(sip_request_user) end
In my lua I have something like above and I want to use the variable sip_request_user1 outside if. What can be done?
Regards, Archana
_________________________________________________________________________ Professional FreeSWITCH Consulting Services: consulting@freeswitch.org (mailto:consulting@freeswitch.org) http://www.freeswitchsolutions.com
FreeSWITCH-powered IP PBX: The CudaTel Communication Server http://www.cudatel.com
Official FreeSWITCH Sites http://www.freeswitch.org http://wiki.freeswitch.org http://www.cluecon.com
FreeSWITCH-users mailing list FreeSWITCH-users@lists.freeswitch.org (mailto:FreeSWITCH-users@lists.freeswitch.org) http://lists.freeswitch.org/mailman/listinfo/freeswitch-users UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users http://www.freeswitch.org
_________________________________________________________________________ Professional FreeSWITCH Consulting Services: consulting@freeswitch.org (mailto:consulting@freeswitch.org) http://www.freeswitchsolutions.com
FreeSWITCH-powered IP PBX: The CudaTel Communication Server http://www.cudatel.com
Official FreeSWITCH Sites http://www.freeswitch.org http://wiki.freeswitch.org http://www.cluecon.com
FreeSWITCH-users mailing list FreeSWITCH-users@lists.freeswitch.org (mailto:FreeSWITCH-users@lists.freeswitch.org) http://lists.freeswitch.org/mailman/listinfo/freeswitch-users UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users http://www.freeswitch.org
The 'local' keyword makes the variable local to the code block it's in. Simply redeclare that variable in the scope you need it available. hunmonk On Monday, November 26, 2012 at 8:29 AM, Archana Venugopan wrote:
Hi,
Does anyone know how can we use a variable declared inside if statement outside if block?
if sip_request_user == nil then sip_request_user = params:getHeader("sip_request_user") else local sip_request_user1 = tonumber(sip_request_user) end
In my lua I have something like above and I want to use the variable sip_request_user1 outside if. What can be done?
Regards, Archana
_________________________________________________________________________ Professional FreeSWITCH Consulting Services: consulting@freeswitch.org (mailto:consulting@freeswitch.org) http://www.freeswitchsolutions.com
FreeSWITCH-powered IP PBX: The CudaTel Communication Server http://www.cudatel.com
Official FreeSWITCH Sites http://www.freeswitch.org http://wiki.freeswitch.org http://www.cluecon.com
FreeSWITCH-users mailing list FreeSWITCH-users@lists.freeswitch.org (mailto:FreeSWITCH-users@lists.freeswitch.org) http://lists.freeswitch.org/mailman/listinfo/freeswitch-users UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users http://www.freeswitch.org
classical variable scope problem. :-) http://lua-users.org/wiki/ScopeTutorial Basically, you should declare variable above and outside IF condition and initialize it say 'nil', then change its value in IF per logic, finally outside below IF block check if variable value is other then 'nil', if so then do your operation. This is very basic programming man! Thank you. On Mon, Nov 26, 2012 at 5:29 PM, Archana Venugopan <a.venugopan@mundio.com>wrote:
Hi,****
** **
Does anyone know how can we use a variable declared inside if statement outside if block?****
** **
if sip_request_user == nil then****
sip_request_user = params:getHeader("sip_request_user")****
else****
local sip_request_user1 = tonumber(sip_request_user)****
end****
** **
In my lua I have something like above and I want to use the variable sip_request_user1 outside if. What can be done?****
** **
Regards,****
Archana****
** **
_________________________________________________________________________ Professional FreeSWITCH Consulting Services: consulting@freeswitch.org http://www.freeswitchsolutions.com
FreeSWITCH-powered IP PBX: The CudaTel Communication Server http://www.cudatel.com
Official FreeSWITCH Sites http://www.freeswitch.org http://wiki.freeswitch.org http://www.cluecon.com
FreeSWITCH-users mailing list FreeSWITCH-users@lists.freeswitch.org http://lists.freeswitch.org/mailman/listinfo/freeswitch-users UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users http://www.freeswitch.org
-- Muhammad Shahzad ----------------------------------- CISCO Rich Media Communication Specialist (CRMCS) CISCO Certified Network Associate (CCNA) Cell: +49 176 99 83 10 85 MSN: shari_786pk@hotmail.com Email: shaheryarkh@googlemail.com
participants (6)
-
Archana Venugopan
-
Bryant Lee
-
Chad Phillips
-
Michael Collins
-
Muhammad Shahzad
-
Steven Ayre