String Comparison Problems

Hi All,

I’m having a baffling problem with string comparisons in Ruby.

The following code just does not act as expected.
measures_full = {‘PROP 1A’=>‘Safe, Reliable High-Speed Train Bond
Act’,
‘PROP 2’ =>‘Standards for Confining Farm Animals’,
‘PROP 3’ =>‘Children’s Hospital Bond Act. Grant
Program.’,
‘PROP 4’ =>‘Parent Notif. Before Terminating Minor’s
Pregnancy’,
‘PROP 5’ =>‘Nonviolent Drug Offense. Sentencing,
Parole, Rehab’,
‘PROP 6’ =>‘Police, Law Enforcement Funding. Criminal
Laws.’,
‘PROP 7’ =>‘Renewable Energy Generation’,
‘PROP 8’ =>‘Eliminates Right of Same-Sex Couples to
Marry’,
‘PROP 9’ =>‘Criminal Justice System. Victims’ Rights.
Parole.’,
‘PROP 10’=>‘Altern. Fuel Vehicles and Renewable Energy
Bonds’,
‘PROP 11’=>‘Redistricting’,
‘PROP 12’=>“Veterans’ Bond”}
fixed_ca = Array.new
prop = String.new
lines = Array.new
i = 1;
j = 0;

puts ‘’
puts “-----------------------READING------------------------”

Add prop numbers to props for CA feed

open(‘2008-CA_ElectionResults.csv’, ‘r’) do |f|
line = f.read
lines = line.split("\n") # Not really sure why this is necessary,
# but for some reason its the only way to
# get each line into its own place in the
array.
j += 1
#puts j
end

puts “#{lines.length} entries”

lines.each do |line|
vals = line.split(’|’)

measures_full.each do |key, value|
if value.eql? vals[0]
puts vals[0]
prop = “#{key} - #{value}|#{vals[1]}|#{vals[2]}|#{vals[3]}|
#{vals[4]}|#{vals[5]}|#{vals[6]}|#{vals[7]}|#{vals[8]}|#{vals[9]}”
puts prop
break
else
prop = line
break
end
end
#puts prop
fixed_ca << prop
i = i + 1
end

puts ‘’
puts “-----------------------WRITING------------------------”
open(‘2008-CA_Fixed.csv’, ‘w’) do |f|
fixed_ca.each do |e|
f.puts e
end
end

The file I’m opening contains:
U.S. President|0|0||Barack Obama|Democratic|9075132|58.1|09/24/2008|
12:38:04
U.S. President|0|0||John McCain|Republican|2251793|14.4|09/24/2008|
12:38:04
U.S. President|0|0||Alan Keyes|American Independent|2278411|14.6|
09/24/2008|12:38:04
U.S. President|0|0||Cynthia McKinney|Green|998072|6.3|09/24/2008|
12:38:04
U.S. President|0|0||Bob Barr|Libertarian|674352|4.3|09/24/2008|
12:38:04
U.S. President|0|0||Ralph Nader|Peace and Freedom|362310|2.3|
09/24/2008|12:38:04
State Senate District 39|0|0||Christine Kehoe|Democratic|136932|33.0|
09/24/2008|12:38:04
State Senate District 39|0|0||Jeff Perwin|Republican|261331|63.0|
09/24/2008|12:38:04
State Senate District 39|0|0||Jesse Thomas|Libertarian|16681|4.0|
09/24/2008|12:38:04
State Assembly District 66|0|0||Grey Kenneth Frandsen|Democratic|
122204|60.7|09/24/2008|12:38:04
State Assembly District 66|0|0||Kevin D. Jeffries|Republican|79276|
39.3|09/24/2008|12:38:04
State Assembly District 73|0|0||Judy Jones|Democratic|116610|53.4|
09/24/2008|12:38:04
State Assembly District 73|0|0||Diane L. Harkey|Republican|92806|42.4|
09/24/2008|12:38:04
State Assembly District 73|0|0||Andrew “Andy” Favor|Libertarian|9332|
4.2|09/24/2008|12:38:04
State Assembly District 74|0|0||Brett Deforest Maxfield|Democratic|
28453|12.9|09/24/2008|12:38:04
State Assembly District 74|0|0||Martin Garrick|Republican|129486|59.2|
09/24/2008|12:38:04
State Assembly District 74|0|0||Paul King|Libertarian|60934|27.9|
09/24/2008|12:38:04
State Assembly District 75|0|0||Darren Kasai|Democratic|214795|93.1|
09/24/2008|12:38:04
State Assembly District 75|0|0||Nathan Fletcher|Republican|808|0.3|
09/24/2008|12:38:04
State Assembly District 75|0|0||John M.|Libertarian|15359|6.6|
09/24/2008|12:38:04
Standards for Confining Farm Animals|0|0||Yes||5200770|6.6|09/24/2008|
12:38:04
Standards for Confining Farm Animals|0|0||No||10439300|6.6|09/24/2008|
12:38:04
Children’s Hospital Bond Act. Grant Program.|0|0||Yes||9756410|6.6|
09/24/2008|12:38:04
Children’s Hospital Bond Act. Grant Program.|0|0||No||5883660|6.6|
09/24/2008|12:38:04
Parent Notif. Before Terminating Minor’s Pregnancy|0|0||Yes||8111423|
6.6|09/24/2008|12:38:04
Parent Notif. Before Terminating Minor’s Pregnancy|0|0||No||7528647|
6.6|09/24/2008|12:38:04
Nonviolent Drug Offense. Sentencing, Parole, Rehab|0|0||Yes||9168058|
6.6|09/24/2008|12:38:04
Nonviolent Drug Offense. Sentencing, Parole, Rehab|0|0||No||6472012|
6.6|09/24/2008|12:38:04
Police, Law Enforcement Funding. Criminal Laws.|0|0||Yes||10148640|6.6|
09/24/2008|12:38:04
Police, Law Enforcement Funding. Criminal Laws.|0|0||No||5491430|6.6|
09/24/2008|12:38:04
Renewable Energy Generation|0|0||Yes||7200459|6.6|09/24/2008|12:38:04
Renewable Energy Generation|0|0||No||8439611|6.6|09/24/2008|12:38:04
Eliminates Right of Same-Sex Couples to Marry|0|0||Yes||7924434|6.6|
09/24/2008|12:38:04
Eliminates Right of Same-Sex Couples to Marry|0|0||No||7715636|6.6|
09/24/2008|12:38:04
Criminal Justice System. Victims’ Rights. Parole.|0|0||Yes||5266581|
6.6|09/24/2008|12:38:04
Criminal Justice System. Victims’ Rights. Parole.|0|0||No||10373489|
6.6|09/24/2008|12:38:04
Safe, Reliable High-Speed Train Bond Act|0|0||Yes||5552432|6.6|
09/24/2008|12:38:04
Safe, Reliable High-Speed Train Bond Act|0|0||No||10087638|6.6|
09/24/2008|12:38:04
Altern. Fuel Vehicles and Renewable Energy Bonds|0|0||Yes||6104326|6.6|
09/24/2008|12:38:04
Altern. Fuel Vehicles and Renewable Energy Bonds|0|0||No||9535744|6.6|
09/24/2008|12:38:04
Redistricting|0|0||Yes||10740910|6.6|09/24/2008|12:38:04
Redistricting|0|0||No||4899160|6.6|09/24/2008|12:38:04
Veterans’ Bond|0|0||Yes||5284610|6.6|09/24/2008|12:38:04
Veterans’ Bond|0|0||No||10355460|6.6|09/24/2008|12:38:04

I SHOULD be getting 24 matches, but for some reason I’m only getting
2 and those are for measure_full[‘PROP 11’]. I’ve even gone so far as
to copy and paste the values from the CSV file into the corresponding
places in the Hash, but it makes no difference, only
measure_full[‘PROP 11’] matches as expected.

mdrisser wrote:

I SHOULD be getting 24 matches, but for some reason I’m only getting
2 and those are for measure_full[‘PROP 11’]. I’ve even gone so far as
to copy and paste the values from the CSV file into the corresponding
places in the Hash, but it makes no difference, only
measure_full[‘PROP 11’] matches as expected.

Looks like the whitespace characters don’t match… maybe try replacing
the whitespaces with a regular space like this:

if value.eql? vals[0].gsub(/\s/, ’ ')

On Tue, 28 Oct 2008, mdrisser wrote:

Hi All,

I’m having a baffling problem with string comparisons in Ruby.

The following code just does not act as expected.
measures_full = {‘PROP 1A’=>‘Safe, Reliable High-Speed Train Bond
Act’,
‘PROP 2’ =>‘Standards for Confining Farm Animals’,
[…]

Add prop numbers to props for CA feed

open(‘2008-CA_ElectionResults.csv’, ‘r’) do |f|
line = f.read
lines = line.split("\n") # Not really sure why this is necessary,
# but for some reason its the only way to
# get each line into its own place in the
array.

Or your could use readlines instead, which would give you the array

straight away. Your data looks simple enough for the CSV library

(or FasterCSV if you need speed) to handle this.

j += 1
#puts j
end

puts “#{lines.length} entries”

lines.each do |line|
vals = line.split(’|’)

measures_full.each do |key, value|
# It might be instructive to :
puts value.dump
puts vals[0].dump
# or puts " #}value.dump} #{vals[0].dump}" if you prefer.

if value.eql? vals[0]

------------------------------------------------------------ Hash#select
hsh.select {|key, value| block} => array

 Returns a new array consisting of +[key,value]+ pairs for which the
 block returns true. Also see +Hash.values_at+.

    h = { "a" => 100, "b" => 200, "c" => 300 }
    h.select {|k,v| k > "a"}  #=> [["b", 200], ["c", 300]]
    h.select {|k,v| v < 200}  #=> [["a", 100]]
  puts vals[0]

fixed_ca << prop

The file I’m opening contains:
U.S. President|0|0||Barack Obama|Democratic|9075132|58.1|09/24/2008|
12:38:04
[…]
State Assembly District 75|0|0||Nathan Fletcher|Republican|808|0.3|
09/24/2008|12:38:04
State Assembly District 75|0|0||John M.|Libertarian|15359|6.6|
09/24/2008|12:38:04

And there’s nothing here to mark the change in data? No count at the
start of the file?

Standards for Confining Farm Animals|0|0||Yes||5200770|6.6|09/24/2008|
12:38:04
Standards for Confining Farm Animals|0|0||No||10439300|6.6|09/24/2008|
12:38:04
[…]
places in the Hash, but it makes no difference, only
measure_full[‘PROP 11’] matches as expected.

    Hugh