Best way to search through an array for an item matching a s

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

Daniel Webb wrote:

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://ruby-doc.org/core/classes/Array.html#M002178

def print_element_before_x_in_y x, y # Shut up. I am GOOD at method
names.
y.index x
x.nil? ? nil : y[x-1]
end

This, minds you, wraps around, so if you find the first item in the
array, it’ll return y[-1] … :wink:

Daniel Webb:

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’

dict = [‘ahoy there’, ‘hello’, ‘me hearties’, ‘dear friends’]
dict[dict.index(‘hello’) - 1] # => ‘ahoy there’

— Shot

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

Thanks Jeremy. That works a treat, however if I’m using an array
populated by values from a text file I get:

undefined method `-’ for nil:NilClass (NoMethodError)

I’ve tried flattening the array but it didn’t make any difference (as
that’s caused issues in the past), but to it’s no use.
Using IRB I can see it’s putting in new line characters when it’s
stripping the line.

i.e. “arrrrrrgh!\n”

I presume this is what is producing the error, does anyone know a way
around this error?

Thanks very much,
Dan

Daniel Webb wrote:

Thanks Jeremy. That works a treat, however if I’m using an array
populated by values from a text file I get:

undefined method `-’ for nil:NilClass (NoMethodError)

Because it’s not finding the string, so the index is nil, and you can’t
subtract from nil.

I’ve tried flattening the array but it didn’t make any difference (as
that’s caused issues in the past),

That’s cargo-cult programming. You’ve got to understand your errors,
not take wild shots in the dark.

but to it’s no use.
Using IRB I can see it’s putting in new line characters when it’s
stripping the line.

i.e. “arrrrrrgh!\n”

I presume this is what is producing the error, does anyone know a way
around this error?

Well, think about it. You can’t find “string” because the array
contains “string\n”. That suggests that you want to remove the \n from
the array. How do you do that? (Hint: it’s a one-liner.)

Thanks very much,
Dan

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Aldric G. wrote:

def print_element_before_x_in_y x, y # Shut up. I am GOOD at method
names.
index = y.index x
index.nil? ? nil : y[x-1]
end

What? I proofread my code.

Thanks I need to just stare at that API for a lot longer…

“.chomp”

Thank you very much