danbaron
15-10-2010, 20:44
[font=courier new][size=8pt]Here is a Ruby program which uses the standard library class, set, in a function which determines if a passed string qualifies as a valid thinBasic variable name.
I think there is not much difference between this script, and the Python version (http://community.thinbasic.com/index.php?topic=3720.msg27313;topicseen#msg27313).
(Python and Ruby, who can say which is, "better"? So far, I sure can't.)
:oops: :x :twisted:
Dan
#------------------------------------------------------------------------------------------------------------
require 'set'
#---------------------------------------------------------------------------------------------------
# file = "varnames.rb"
# date: 2010-10-15
# This is a Ruby script that determines which in a list of identifiers (strings) are valid thinBasic variable names.
# If you download it, change its name from, "varnames.rb.txt", to, "varnames.rb".
# To run it, you need Ruby 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, "ruby varnames.rb".
#---------------------------------------------------------------------------------------------------
# thinBasic variable names are case insensitive.
# thinBasic variable names can be any length greater than 0.
# Currently, the last character in a variable name cannot be, '_'.
# 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 must be either, 'a-z', 'A-Z', or, '0-9'.
#------------------------------------------------------------------------------------------------------------
# functions
def setsets
s = '_ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$firstchar = Set.new(s.split(''))
s = '0123456789'
$midchar = Set.new(s.split(''))
$midchar = $midchar.union($firstchar)
$lastchar = Set.new()
$lastchar = $lastchar.union($midchar)
$lastchar.delete('_')
end
def validvariablename?(s)
s = s.upcase
ll = s.length
return false if ll == 0
l = Array.new(s.split(''))
i = 1
for c in l do
if not ($firstchar.include?(c))
return false
end if i == 1
if not ($midchar.include?(c))
return false
end if i > 1 and i < ll
if not ($lastchar.include?(c))
return false
end if i == ll
i += 1
end
return true
end
#------------------------------------------------------------------------------------------------------------
# program
setsets
n = []
n[0] = ''
n[1] = '_'
n[2] = 'a_'
n[3] = '6'
n[4] = 'd'
n[5] = '_q'
n[6] = '_____________________________________________________6'
n[7] = '____________________________________a___________________________________________b'
n[8] ='gjtuhjy897058#bnguy87'
n[9] = 'TMGIUKJ_fhrydhe_6978576_3867thgy_HTY6UY87IU'
n[10] = 'YUHJTIGKT9687UYJHUYIRHFJGNVMBJGUT&DURHFYT76895'
n[11] = '$'
n[12] = 'biykh9708ou968turhfyt758395867fhvnbmgkhiukjotlgpedhtyfhcnghtyr856903euthdrut8679whdbcnvjgktoy978463tergdhfncmvjfgur86otiyughdjnvqyrhgnvmbjguy8679486849586uyjhkfmvhgnbutkfieldotiykhiukfjhmbnv78yi0tuy76urhfjhmvnbhgyturjdhfyrht78590uijkhmnljouitehdfgvnbmhjyito7980fhrudjnvmbkhiykhoedj5utjguyjhiukjmnjhnbgjhuy7uryehdgfbvhguy87958374ythfnvgjbmhkuiykdcnvhfngytu6759123dgrtdgvnghbjhmnkjiuolpdrujgkyihknmbjguhyjtoedjgnvmbjhuy48tuyjhgmbnkhiukjoedplazmcnvhgythfjguyjhitkgolrgjhmbjhuyjgutyikolpedjgmbjhmnkhiykhiu8975tuyjhu68793edjgnbmgjhmnkjiuf'
puts
puts 'Valid thinBasic variable name?'
puts
i = 0
for nm in n do
print i, " "
puts validvariablename?(nm)
i += 1
end
readline
#------------------------------------------------------------------------------------------------------------
I think there is not much difference between this script, and the Python version (http://community.thinbasic.com/index.php?topic=3720.msg27313;topicseen#msg27313).
(Python and Ruby, who can say which is, "better"? So far, I sure can't.)
:oops: :x :twisted:
Dan
#------------------------------------------------------------------------------------------------------------
require 'set'
#---------------------------------------------------------------------------------------------------
# file = "varnames.rb"
# date: 2010-10-15
# This is a Ruby script that determines which in a list of identifiers (strings) are valid thinBasic variable names.
# If you download it, change its name from, "varnames.rb.txt", to, "varnames.rb".
# To run it, you need Ruby 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, "ruby varnames.rb".
#---------------------------------------------------------------------------------------------------
# thinBasic variable names are case insensitive.
# thinBasic variable names can be any length greater than 0.
# Currently, the last character in a variable name cannot be, '_'.
# 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 must be either, 'a-z', 'A-Z', or, '0-9'.
#------------------------------------------------------------------------------------------------------------
# functions
def setsets
s = '_ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$firstchar = Set.new(s.split(''))
s = '0123456789'
$midchar = Set.new(s.split(''))
$midchar = $midchar.union($firstchar)
$lastchar = Set.new()
$lastchar = $lastchar.union($midchar)
$lastchar.delete('_')
end
def validvariablename?(s)
s = s.upcase
ll = s.length
return false if ll == 0
l = Array.new(s.split(''))
i = 1
for c in l do
if not ($firstchar.include?(c))
return false
end if i == 1
if not ($midchar.include?(c))
return false
end if i > 1 and i < ll
if not ($lastchar.include?(c))
return false
end if i == ll
i += 1
end
return true
end
#------------------------------------------------------------------------------------------------------------
# program
setsets
n = []
n[0] = ''
n[1] = '_'
n[2] = 'a_'
n[3] = '6'
n[4] = 'd'
n[5] = '_q'
n[6] = '_____________________________________________________6'
n[7] = '____________________________________a___________________________________________b'
n[8] ='gjtuhjy897058#bnguy87'
n[9] = 'TMGIUKJ_fhrydhe_6978576_3867thgy_HTY6UY87IU'
n[10] = 'YUHJTIGKT9687UYJHUYIRHFJGNVMBJGUT&DURHFYT76895'
n[11] = '$'
n[12] = 'biykh9708ou968turhfyt758395867fhvnbmgkhiukjotlgpedhtyfhcnghtyr856903euthdrut8679whdbcnvjgktoy978463tergdhfncmvjfgur86otiyughdjnvqyrhgnvmbjguy8679486849586uyjhkfmvhgnbutkfieldotiykhiukfjhmbnv78yi0tuy76urhfjhmvnbhgyturjdhfyrht78590uijkhmnljouitehdfgvnbmhjyito7980fhrudjnvmbkhiykhoedj5utjguyjhiukjmnjhnbgjhuy7uryehdgfbvhguy87958374ythfnvgjbmhkuiykdcnvhfngytu6759123dgrtdgvnghbjhmnkjiuolpdrujgkyihknmbjguhyjtoedjgnvmbjhuy48tuyjhgmbnkhiukjoedplazmcnvhgythfjguyjhitkgolrgjhmbjhuyjgutyikolpedjgmbjhmnkhiykhiu8975tuyjhu68793edjgnbmgjhmnkjiuf'
puts
puts 'Valid thinBasic variable name?'
puts
i = 0
for nm in n do
print i, " "
puts validvariablename?(nm)
i += 1
end
readline
#------------------------------------------------------------------------------------------------------------