From: Christer Nilsson Date: 2006-01-09T19:16:07+09:00 Subject: Re: simple match of items in 2 arrays Charles L. Snyder wrote: > I can't get the comparison to work, and don't know what format to put > the result into (hash, array of arrays, file) - from which I can > easily output the results (ie., lat long, city, state) by zipcode... > > Any guidance appreciated.. Charles, I don't konw if this is the best way of doing it, but it works at least. btw, your file has errors. It the code supposed to deal with them? e.g. commas in the latitude, spaces after minus. Have you considered using a database table? Christer class ZipCodeReader ZipCode = Struct.new(:zipcode, :lat, :lng, :address, :state, :city, :freq) def read(filename) @zipcode = {} IO.foreach(filename) do |line| arr = line.chomp.split(",") zipcode = ZipCode.new(arr[0], arr[1].to_f, arr[2].to_f, arr[3].lstrip, arr[4].lstrip, arr[5].lstrip, 0) @zipcode[arr[0]] = zipcode end end def readfreq(filename) IO.foreach(filename) do |line| arr = line.chomp.split(",") @zipcode[arr[0]].freq = arr[1].lstrip.to_i unless @zipcode[arr[0]].nil? end end def dump arr=@zipcode.sort arr.each {|x| print x.to_s + "\n"} end end zipcode=ZipCodeReader.new zipcode.read("zipcode.txt") zipcode.readfreq("zipdata.txt") zipcode.dump -- Posted via http://www.ruby-forum.com/.