From: William James Date: 2008-02-12T19:54:58+09:00 Subject: Re: non case sensitive searching On Feb 12, 3:39 am, Robert Klemme wrote: > 2008/2/12, William James : > > > > > On Feb 12, 1:45 am, Robert Klemme wrote: > > > 2008/2/12, Adam Akhtar : > > > > > the reg exp. looks good but when i try to apply it to help me romove > > > > duplicates from a list it doesnt seem to work > > > > > list = %w{adam Adam bobby Bobby wild wILd} > > > > list.sort > > > > The line above is ineffective because you do not sort the original list. > > > > > list.each_index do |x| > > > > list.delete_at(x) if (/list[x]/i =~ list[x+1]) #remove entries which > > > > have same spelling but in diff. case > > > > end > > > > puts "" > > > > puts list > > > > I would use another algorithm because of efficiency: > > > > #!/bin/env ruby > > > > require 'set' > > > > # ensure random order > > > list = %w{adam Adam bobby Bobby wild wILd}.sort_by { rand } > > > dups = Set.new > > > > p list > > > list.delete_if {|w| not dups.add? w.downcase } > > > p list > > > We don't need sets for this. > > > list = %w{adam Adam bobby Bobby wild wILd}.sort_by { rand } > > ==>["wild", "bobby", "Bobby", "wILd", "Adam", "adam"] > > list.map{|x| x.upcase}.uniq > > ==>["WILD", "BOBBY", "ADAM"] > > As far as I can see the requirement was to remove duplicates and not > to output a uniform cased list. 1. My solution removed duplicates. 2. To output a uniform-cased list does no harm since case is irrelevant. Using plain Ruby is shorter, easier, and clearer. There's no rational reason to require sets. Learn to use Ruby. It's a very powerful language; so powerful, in fact, that I seldom have to use external libraries.