From: Jeremy Woertink Date: 2009-11-24T01:56:57+09:00 Subject: Re: Best way to search through an array for an item matching a s Daniel Webb wrote: > Hi everyone, > > > am trying to find a nice easy/simple way to > - search through an array of strings match the string I've input > - use that index to print out the word before in the array. > > for example > [ahoy there,hello,me hearties,dear friends] > > 'hello' should output 'ahoy there' > > http://pastie.org/711384 > > I'm hoping there's a way of doing this without regexp... > > > Thanks very much, > > Dan ary = ["hello", "hey", "hi", "hola"] ary[ary.index("hey") - 1] #=> "hello" ary[ary.index("hello") - 1] #=> "hola" ary.index("sup") #=> nil Maybe that will give ya a bit of a boost! ~Jeremy -- Posted via http://www.ruby-forum.com/.