From: Robert Klemme Date: 2006-05-06T05:28:28+09:00 Subject: Re: Newbie question 2006/5/5, Kyle Alons : > James Gray wrote: > > >> ["&abc", "a&cb", "b&ac", "ccc", "cb&a"].grep(/&../) > > => ["&abc", "a&cb", "b&ac"] > > > > Hope that helps. > > That's not quite it. That seems to match all strings containing an > ampersand followed by 2 other characters. What I need is all strings > that have duplicate characters following an & (&a in the above). By your definition "a&cb" does not belong into the result set. > Something a little more concrete: > > ["&File", "&Edit", "Vi&ew", "P&roject", "T&ree", "Favo&rites", "&Help"] > > would return > > ["&Edit", "Vi&ew", "P&roject", "T&ree", "Favo&rites"] > > (&e and &r are found multiple times). Just for the fun of it: irb(main):037:0> a=["&File", "&Edit", "Vi&ew", "P&roject", "T&ree", "Favo&rites", "&Help"] => ["&File", "&Edit", "Vi&ew", "P&roject", "T&ree", "Favo&rites", "&Help"] irb(main):038:0> a.inject(Hash.new {|h,k| h[k]=[]}) do |h,str| irb(main):039:1* /&(.)/ =~ str and h[$1.downcase[0]] << str irb(main):040:1> h irb(main):041:1> end.values.select {|v| v.size > 1}.flatten => ["&Edit", "Vi&ew", "P&roject", "T&ree", "Favo&rites"] irb(main):042:0> a.inject(Hash.new {|h,k| h[k]=[]}) do |h,str| irb(main):043:1* /&(.)/ =~ str and h[$1.downcase[0]] << str irb(main):044:1> h irb(main):045:1> end.inject([]){|res,(k,v)| res.concat v if v.size > 1;res } => ["&Edit", "Vi&ew", "P&roject", "T&ree", "Favo&rites"] Did I mention that inject is great? :-)) robert -- Have a look: http://www.flickr.com/photos/fussel-foto/