From: RichardOnRails Date: 2007-11-19T14:39:59+09:00 Subject: False positives in editing data 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 = <> "; (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