Results 1 to 1 of 1

Thread: Lua Sets

  1. #1
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    152

    Lua Sets

    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/s...0844-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()
    
    Attached Files Attached Files
    Last edited by danbaron; 12-11-2010 at 09:51.
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

Similar Threads

  1. Perl Sets
    By danbaron in forum Scripting
    Replies: 1
    Last Post: 26-10-2010, 02:04
  2. Python Sets
    By danbaron in forum Scripting
    Replies: 5
    Last Post: 24-10-2010, 06:10
  3. Ruby Sets
    By danbaron in forum Scripting
    Replies: 0
    Last Post: 15-10-2010, 20:44
  4. Infinite Sets
    By danbaron in forum Math: all about
    Replies: 7
    Last Post: 28-01-2010, 10:01

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •