From: Brett Simmers Date: 2007-07-21T05:41:49+09:00 Subject: Re: Regexp question --------------060309070306060900070404 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit darren kirby wrote: > quoth the bcparanj@gmail.com: > >> I have a list of zip codes (3000). Is there any script that I could >> use to create a regexp for checking if a user entered zip is in this >> list or not? TIA. >> > > Doubtful you need a regexp. Answer depends on the format of your list. If it > is a separate file with one code per line you could do: > > codes = IO.readlines("/path/to/codes").each { |line| line.chomp! } > codes.include?(90210.to_s) > > or if you have ZIP + 4 codes: > > codes.include?("90210-1234") > > -d > Array#include? is slow for large arrays. If you'll be performing more than 1 lookup over the life of the program, you should create a hash: codes = Hash.new IO.readlines("/path/to/codes").each {|code| codes[code.chomp] = true} Then just test with: codes['12345'] Or you could use a Set, which basically does the exact same thing with different syntax: require 'set' codes = Set.new IO.readlines('/path/to/codes").each {|code| set.add(code.chomp)} Test with: set.include?('12345') --------------060309070306060900070404--