From: Hugh Sasse Date: 2008-10-28T09:56:23+09:00 Subject: Re: String Comparison Problems 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', [...] > '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. # 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] > 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 [...] > 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 Murphy|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 [...] > 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. > Hugh