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

9800daba77be079c6c250a9bfe4a8e6c?d=identicon&s=25 Daniel Webb (dan_webb)
on 2009-11-23 17:36
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
76d5176f2419e5984d7680d1951c0b5e?d=identicon&s=25 Aldric Giacomoni (trevoke)
on 2009-11-23 17:51
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] ... ;-)
76d5176f2419e5984d7680d1951c0b5e?d=identicon&s=25 Aldric Giacomoni (trevoke)
on 2009-11-23 17:51
Aldric Giacomoni 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.
Cec345a59245af9d06e4438a413f4eb5?d=identicon&s=25 Shot (Piotr Szotkowski) (Guest)
on 2009-11-23 17:54
(Received via mailing list)
Attachment: signature.asc (198 Bytes)
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
899f97295621824a5f482a286f5062ca?d=identicon&s=25 Jeremy Woertink (nuby2ruby)
on 2009-11-23 17:56
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
9800daba77be079c6c250a9bfe4a8e6c?d=identicon&s=25 Daniel Webb (dan_webb)
on 2009-11-24 12:55
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
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-11-24 15:57
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
marnen@marnen.org
9800daba77be079c6c250a9bfe4a8e6c?d=identicon&s=25 Daniel Webb (dan_webb)
on 2009-11-24 18:34
Thanks I need to just stare at that API for a lot longer...

".chomp"

Thank you very much
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.