2007/12/27, MonkeeSage <MonkeeSage / gmail.com>: > On Dec 26, 10:20 pm, Esmail <ebonak_de... / hotmail.com> wrote: > > MonkeeSage wrote: > > > On Dec 26, 4:32 pm, Esmail <ebonak_de... / hotmail.com> wrote: > > >> If I have an array ar of strings that contains > > >> for instance > > > > >> aaaa > > >> bbbb > > >> cccc > > >> >dddd > > >> eeee > > >> dddd > > >> cccc > > >> etc. > > > > >> is there a way to use ar.index with a regular > > >> expression to get the index of the line >dddd > > > > >> I've tried ar.index(/^>/) and (/^\>/) without > > >> much luck. > > > > >> In other words, I'm trying to match on the first > > >> character which is a > > > > > >> Thanks. > > > > > ['aaaa', '>bbbb', 'cccc'].find { | e | e =~ /^>/ } > > > > Hi Jordan, > > > > Is there a way to use this regular expression to return the > > index value of the position where this string is found? That > > is the main thing I am interested in. > > > > It seems there ought to be an easy way ('cept I don't know it :-) > > > > Esmail > > Hi, > > There's no built-in way that I'm aware of. How about this one - kind of pseudo built in. :-) irb(main):007:0> a=['aaaa', '>bbbb', 'cccc'] => ["aaaa", ">bbbb", "cccc"] irb(main):008:0> a.to_enum(:each_with_index).find {|e,i| /^>/ =~ e}.last => 1 A similar approach also works when looking for multiple indexes: irb(main):009:0> a.to_enum(:each_with_index).select {|e,i| /^>|c+/ =~ e}.map {|e,i| i} => [1, 2] But I agree, usually indexes are fairly seldom needed with Arrays. Kind regards robert -- use.inject do |as, often| as.you_can - without end