Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Best way to search through an array for an item matching a s

Reply
Thread Tools

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

 
 
Daniel Webb
Guest
Posts: n/a
 
      11-23-2009
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Aldric Giacomoni
Guest
Posts: n/a
 
      11-23-2009
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] ...
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Aldric Giacomoni
Guest
Posts: n/a
 
      11-23-2009
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jeremy Woertink
Guest
Posts: n/a
 
      11-23-2009
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/.

 
Reply With Quote
 
Daniel Webb
Guest
Posts: n/a
 
      11-24-2009
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      11-24-2009
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Daniel Webb
Guest
Posts: n/a
 
      11-24-2009
Thanks I need to just stare at that API for a lot longer...

".chomp"

Thank you very much
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to search through STL maps? Steve C++ 10 12-01-2009 02:38 PM
Best way to loop through ArrayList and remove elements on the way? Kevin Java 16 01-30-2008 08:54 PM
how to search through file for matching criteria richjungk@gmail.com Perl Misc 1 06-08-2006 02:32 AM
Best way to find if item is in array? J. B. Moreno Javascript 8 08-31-2005 09:41 PM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM



Advertisments