On Nov 19, 12:39 am, RichardOnRails <RichardDummyMailbox58... / uscomputergurus.com> wrote: > Hi All, > > Below is a 33-line program that analyzes a set of names. > The names proper are prefixed with an optional set of period-separated > numbers. > The analysis checks for the following, and flags violations: > 1. A number may not start with zeros. > 2. No more that two numbers may be prefixed > 3. Names proper must begin with a non-digit > > The program works pretty well, except it produces "false negatives," > which I flag on the output displayed following the program. The > problem is around line 29 when I try to determine if a leading number > has any initial zero by using an RE and testing for Nil. > > I'd welcome any ideas on how to correct my mistake(s), especially with > suggestion for improved style. > > Thanks in advance, > Richard > > Program > ------------------------------------------------------------------------------- > MLN = 2 # MaxLeadingNumbers > input = <<DATA > 05Topic 5 > 1Topic 1 > 2.002.1Topic 2.2.1 > 2.1Topic 2.1 > 2.2.02Topic 2.2.2 > DATA > input.each { |sName| > # Debug > puts "\n" + "="*10 + "DBG", sName, "="*10+ "DBG" > > #Get leading numbers separated by periods into $1, $2, $13 > sName =~ /^([\d]+)?\.?([\d]+)?\.?([\d]+)?\.?/ > > # Save match variables in n[1] ... n[MLN+1] > n = Array.new > (1..MLN+1).each { |i| eval %{n[i] = $#{i}} } > > # Debug > print "DBG0>> "; (1..MLN+1).each { |i| printf("\t#{n[i]}") }; puts > # puts "n1=#{n[1]}, n2=#{n[2]}, n3=#{n[3]}" > > # Get and check leading, period-separated numbers in dir. names > (1..MLN+1).each { |i| > n[i] =~ /^0+/ > i_thNumberExists = n[i] ? true : false > iThNumbrerHasLeadingZero = i_thNumberExists && (eval("$#{i}.class == > NilClass") ? > true : false) > puts "ERROR: Leading zeros in #{n[i]}" if iThNumbrerHasLeadingZero > && (i <= MLN) > puts "ERROR: Depth of hierarchy exceeds #{MLN}" if i_thNumberExists > && i > MLN > } > > } > > Output > --------------------------------------------- > > ==========DBG > 05Topic 5 > ==========DBG > DBG0>> 05 > ERROR: Leading zeros in 05 > > ==========DBG > 1Topic 1 > ==========DBG > DBG0>> 1 > ERROR: Leading zeros in 1 [False positive] > > ==========DBG > 2.002.1Topic 2.2.1 > ==========DBG > DBG0>> 2 002 1 > ERROR: Leading zeros in 2 [False positive] > ERROR: Leading zeros in 002 > ERROR: Depth of hierarchy exceeds 2 > > ==========DBG > 2.1Topic 2.1 > ==========DBG > DBG0>> 2 1 > ERROR: Leading zeros in 2 [False positive] > ERROR: Leading zeros in 1 [False positive] > > ==========DBG > 2.2.02Topic 2.2.2 > ==========DBG > DBG0>> 2 2 02 > ERROR: Leading zeros in 2 [False positive] > ERROR: Leading zeros in 2 [False positive] > ERROR: Depth of hierarchy exceeds 2 > > >Exit code: 0 Hi All, Problem solved! I went through a convoluted process to determine whether any of the initial numbers began with a zero. I simplified it to: i_thNumberHasLeadingZero = (n[i] =~ /^0+/) ? true : false I apologize for raising the question. My excuse is that I was flailing about unable to figure out what was wrong. After I posted, I took a harder look and realized my big mistake: I needed to use the KISS method :-) Best wishes, Richard