Regexp question

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.

quoth the [email protected]:

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

darren kirby wrote:

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’)