From: matt@... (matt neuburg) Date: 2007-08-25T03:50:04+09:00 Subject: Re: Selecting indices from an array William James wrote: > On Aug 23, 8:08 am, "Ronald Fischer" > wrote: > > > If this is a > > > problem, you can use this: > > > > > a.enum_for(:each_with_index).inject([]){|res, i| > > > i[0].match( /g$/) ? res << i[1] : res > > > } > > > > This is an idea I like! Thanks a lot! > > > > Ronald > > -- > > Ronald Fischer > > Phone: +49-89-452133-162 > > accum = [] > a.each_with_index{|s,i| accum << i if s =~ /g$/ } And to generalize further, let's abstract the test (it shouldn't matter what the test is, just as it shouldn't matter what the array is): #specific part a=%w(Deutschlandsberg Stainz Preding Eibiswald) test = Proc.new {|s| s=~/g$/} #general part result = [] a.each_with_index {|ss,ix| result << ix if test.call(ss)} Or, even more general (because the names stop mattering): def indices_matching_test(a) result = [] a.each_with_index {|ss,ix| result << ix if yield ss} result end #and here's how to use it: a=%w(Deutschlandsberg Stainz Preding Eibiswald) test = Proc.new {|s| s=~/g$/} puts indices_matching_test(a, &test) m. -- matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/ Tiger - http://www.takecontrolbooks.com/tiger-customizing.html AppleScript - http://www.amazon.com/gp/product/0596102119 Read TidBITS! It's free and smart. http://www.tidbits.com