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

D

Daniel Webb

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
 
A

Aldric Giacomoni

Daniel said:
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] ... ;-)
 
J

Jeremy Woertink

Daniel said:
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
 
D

Daniel Webb

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
 
M

Marnen Laibow-Koser

Daniel said:
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,
 
D

Daniel Webb

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

".chomp"

Thank you very much
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top