From: Ammar Ali Date: 2011-12-03T20:44:39+09:00 Subject: Re: Is there a better way to check this array? On Sat, Dec 3, 2011 at 1:06 PM, Wayne Brissette wrote: > I have an array which contains items that need to be renamed. Unfortunately the way things are changed are based on a comparison of if something else exists. For example in this simple array: > >  names = ['sue,5', 'tom,6', 'jim,7', 'sally,8', 'fred,9', 'barney,0'] > > > if my logic was if look at jim, see if fred exists in array, if so, change jim to Jim_Brown. > > As I see it now I can't do a simple include? because my array isn't split properly. I've tried doing a grep, but that also doesn't work. > > ruby-1.9.2-p290 :020 > names = ['sue,5', 'tom,6', 'jim,7', 'sally,8', 'fred,9', 'barney,0'] >  => ["sue,5", "tom,6", "jim,7", "sally,8", "fred,9", "barney,0"] > ruby-1.9.2-p290 :021 > names.include?('fred') >  => false > ruby-1.9.2-p290 :022 > names.grep('fred') >  => [] grep should do what you want with a regular expression. Here are few examples that you can try: >> names.grep /fred/ => ["fred,9"] >> names.grep /^fred/ => ["fred,9"] >> names.grep /^fred,\d+/ => ["fred,9"] >> names.grep /^fred,\d+/i => ["fred,9"] >> names.grep /\Afred,\d+\z/i => ["fred,9"] Regards, Ammar