danbaron
12-11-2010, 07:10
Here is a Lua program which demonstrates a function that determines if a passed string qualifies as a valid thinBasic variable name.
It is the Lua equivalent of this previous Perl program.
http://www.thinbasic.com/community/showthread.php?10844-Perl-Sets
Dan
-----------------------------------------------------------------------------------------------------
-- file = "varnames.lua"
-- date: 2010-11-12
-----------------------------------------------------------------------------------------------------
-- This is a Lua script that determines which in a list of identifiers (strings) are valid thinBasic variable names.
-- If you download it, change its name from, "varnames.lua.txt", to, "varnames.lua".
-- To run it, you need Lua installed on your computer.
-- To run it, you can open a command window in the folder that contains the file
-- (shift + right click on the folder, "Open command window here", from the menu).
-- Then, execute the command, "lua varnames.lua".
-----------------------------------------------------------------------------------------------------
-- thinBasic variable names are case insensitive.
-- thinBasic variable names can be any length greater than 0.
-- Currently, the last character in a variable name, maybe should not be, '_' (sometimes, thinBasic becomes confused, if it is).
-- The first character in a thinBasic variable name must be either, '_', 'a-z', or, 'A-Z'.
-- A middle character in a thinBasic variable name must be either, '_', 'a-z', 'A-Z', or, '0-9'.
-- The last character in a thinBasic variable name should probably be either, 'a-z', 'A-Z', or, '0-9'.
-----------------------------------------------------------------------------------------------------
function setsets()
firstchar = {}
midchar = {}
lastchar = {}
local i, c
local sf = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local sm = sf .. "0123456789"
local sl = sm
sl = string.gsub(sl,"_","")
for i = 1, sf:len() do
c = sf:sub(i, i)
firstchar[c] = true
end
for i = 1, sm:len() do
c = sm:sub(i, i)
midchar[c] = true
end
for i = 1, sl:len() do
c = sl:sub(i, i)
lastchar[c] = true
end
end
function setnames()
names = {}
names[1] = ''
names[2] = '_'
names[3] = 'a_'
names[4] = '6'
names[5] = 'd'
names[6] = '_q'
names[7] = '_____________________________________________________6'
names[8] = '____________________________________a___________________________________________b'
names[9] ='gjtuhjy897058--bnguy87'
names[10] = 'TMGIUKJ_fhrydhe_6978576_3867thgy_HTY6UY87IU'
names[11] = 'YUHJTIGKT9687UYJHUYIRHFJGNVMBJGUT&DURHFYT76895'
names[12] = '$'
names[13] = 'biykh9708ou968turhfyt758395867fhvnbmgkhiukjotlgpedhtyfhcnghtyr856903euthdrut8679whdbcnvjgktoy978463tergdhfncmvjfgur86otiyughdjnvqyrhgnvmbjguy8679486849586uyjhkfmvhgnbutkfieldotiykhiukfjhmbnv78yi0tuy76urhfjhmvnbhgyturjdhfyrht78590uijkhmnljouitehdfgvnbmhjyito7980fhrudjnvmbkhiykhoedj5utjguyjhiukjmnjhnbgjhuy7uryehdgfbvhguy87958374ythfnvgjbmhkuiykdcnvhfngytu6759123dgrtdgvnghbjhmnkjiuolpdrujgkyihknmbjguhyjtoedjgnvmbjhuy48tuyjhgmbnkhiukjoedplazmcnvhgythfjguyjhitkgolrgjhmbjhuyjgutyikolpedjgmbjhmnkhiykhiu8975tuyjhu68793edjgnbmgjhmnkjiuf'
end
function checkname(n)
local c,i,l,nu
l = n:len()
nu = n:upper()
if l == 0 then return false end
for i = 1, l do
c = nu:sub(i,i)
if i==1 then if not firstchar[c] then return false end end
if (i>1 and i<l) then if not midchar[c] then return false end end
if i==l then if not lastchar[c] then return false end end
end
return true
end
function checkallnames()
local i,v
print('\n')
print('Valid thinBasic variable name?\n')
for i,v in ipairs(names) do print(i, checkname(v)) end
print('\n')
end
setsets()
setnames()
checkallnames()
io.read()
It is the Lua equivalent of this previous Perl program.
http://www.thinbasic.com/community/showthread.php?10844-Perl-Sets
Dan
-----------------------------------------------------------------------------------------------------
-- file = "varnames.lua"
-- date: 2010-11-12
-----------------------------------------------------------------------------------------------------
-- This is a Lua script that determines which in a list of identifiers (strings) are valid thinBasic variable names.
-- If you download it, change its name from, "varnames.lua.txt", to, "varnames.lua".
-- To run it, you need Lua installed on your computer.
-- To run it, you can open a command window in the folder that contains the file
-- (shift + right click on the folder, "Open command window here", from the menu).
-- Then, execute the command, "lua varnames.lua".
-----------------------------------------------------------------------------------------------------
-- thinBasic variable names are case insensitive.
-- thinBasic variable names can be any length greater than 0.
-- Currently, the last character in a variable name, maybe should not be, '_' (sometimes, thinBasic becomes confused, if it is).
-- The first character in a thinBasic variable name must be either, '_', 'a-z', or, 'A-Z'.
-- A middle character in a thinBasic variable name must be either, '_', 'a-z', 'A-Z', or, '0-9'.
-- The last character in a thinBasic variable name should probably be either, 'a-z', 'A-Z', or, '0-9'.
-----------------------------------------------------------------------------------------------------
function setsets()
firstchar = {}
midchar = {}
lastchar = {}
local i, c
local sf = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local sm = sf .. "0123456789"
local sl = sm
sl = string.gsub(sl,"_","")
for i = 1, sf:len() do
c = sf:sub(i, i)
firstchar[c] = true
end
for i = 1, sm:len() do
c = sm:sub(i, i)
midchar[c] = true
end
for i = 1, sl:len() do
c = sl:sub(i, i)
lastchar[c] = true
end
end
function setnames()
names = {}
names[1] = ''
names[2] = '_'
names[3] = 'a_'
names[4] = '6'
names[5] = 'd'
names[6] = '_q'
names[7] = '_____________________________________________________6'
names[8] = '____________________________________a___________________________________________b'
names[9] ='gjtuhjy897058--bnguy87'
names[10] = 'TMGIUKJ_fhrydhe_6978576_3867thgy_HTY6UY87IU'
names[11] = 'YUHJTIGKT9687UYJHUYIRHFJGNVMBJGUT&DURHFYT76895'
names[12] = '$'
names[13] = 'biykh9708ou968turhfyt758395867fhvnbmgkhiukjotlgpedhtyfhcnghtyr856903euthdrut8679whdbcnvjgktoy978463tergdhfncmvjfgur86otiyughdjnvqyrhgnvmbjguy8679486849586uyjhkfmvhgnbutkfieldotiykhiukfjhmbnv78yi0tuy76urhfjhmvnbhgyturjdhfyrht78590uijkhmnljouitehdfgvnbmhjyito7980fhrudjnvmbkhiykhoedj5utjguyjhiukjmnjhnbgjhuy7uryehdgfbvhguy87958374ythfnvgjbmhkuiykdcnvhfngytu6759123dgrtdgvnghbjhmnkjiuolpdrujgkyihknmbjguhyjtoedjgnvmbjhuy48tuyjhgmbnkhiukjoedplazmcnvhgythfjguyjhitkgolrgjhmbjhuyjgutyikolpedjgmbjhmnkhiykhiu8975tuyjhu68793edjgnbmgjhmnkjiuf'
end
function checkname(n)
local c,i,l,nu
l = n:len()
nu = n:upper()
if l == 0 then return false end
for i = 1, l do
c = nu:sub(i,i)
if i==1 then if not firstchar[c] then return false end end
if (i>1 and i<l) then if not midchar[c] then return false end end
if i==l then if not lastchar[c] then return false end end
end
return true
end
function checkallnames()
local i,v
print('\n')
print('Valid thinBasic variable name?\n')
for i,v in ipairs(names) do print(i, checkname(v)) end
print('\n')
end
setsets()
setnames()
checkallnames()
io.read()