A couple issues here: 1. I think John is right in that the break is causing the problem, but... 2. That loop is unnecessary. Why create a hash and then not use it to lookup the props? 3. In ruby, there are a couple of different 'open' methods, if you use File.open(), '#each' will give you an iterator over each line. All of the methods in Enumerable are based on each, so you have lots of options for operating on lines. 4. Is you use CSV (suggested above) or FasterCSV, you can easily map names to the columns, which makes processing the data a bit easier.
Here is a version of what I think you are trying to do: http://gist.github.com/21093 Take a look at this and read through the online api docs. For this case, File, Array and Enumerable should get you pretty far. welcome to ruby! Brian On Oct 27, 12:49 pm, mdrisser <[EMAIL PROTECTED]> wrote: > Hi all, > As the subject says, I'm new to Ruby, though not to programming (Perl, > Python, PHP, JavaScript, bash, etc). The problem I'm having is that > comparing 2 strings is not working as expected. I have a hash that I'm > comparing to an array generated from a file, I *should* be getting 24 > matches, but I'm only getting 2. > > Here's the code I'm using: > 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| > ### This is where things fall apart ### > 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 > > And the CSV is: > 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 Murphy|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've copied and pasted the values from the CSV file to the values for > the Hash, but even that doesn't help. I've tried comparing the classes > for 'values' and 'vals[0]' the are the same in every case. I've tried > '==' but get the same results, which from what I understand is to be > expected. I'm just baffled as to why only 2 matches out of the 24 I'm > expecting. > > Thanks in advance for your help on this. > Michael D. Risser > Web Developer --~--~---------~--~----~------------~-------~--~----~ SD Ruby mailing list [email protected] http://groups.google.com/group/sdruby -~----------~----~----~----~------~----~------~--~---
