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

Discussion in 'Ruby' started by Daniel Webb, Nov 23, 2009.

  1. Daniel Webb

    Daniel Webb Guest

    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/.
    Daniel Webb, Nov 23, 2009
    #1
    1. Advertising

  2. 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/.
    Aldric Giacomoni, Nov 23, 2009
    #2
    1. Advertising

  3. 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/.
    Aldric Giacomoni, Nov 23, 2009
    #3
  4. 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/.
    Jeremy Woertink, Nov 23, 2009
    #4
  5. Daniel Webb

    Daniel Webb Guest

    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/.
    Daniel Webb, Nov 24, 2009
    #5
  6. Re: Best way to search through an array for an item matching

    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

    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Nov 24, 2009
    #6
  7. Daniel Webb

    Daniel Webb Guest

    Re: Best way to search through an array for an item matching

    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/.
    Daniel Webb, Nov 24, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin
    Replies:
    16
    Views:
    47,229
    Roedy Green
    Jan 30, 2008
  2. Steve
    Replies:
    10
    Views:
    802
  3. Abby Lee
    Replies:
    5
    Views:
    376
    Abby Lee
    Aug 2, 2004
  4. Replies:
    1
    Views:
    116
    John W. Krahn
    Jun 8, 2006
  5. J. B. Moreno

    Best way to find if item is in array?

    J. B. Moreno, Aug 31, 2005, in forum: Javascript
    Replies:
    8
    Views:
    105
    Jim Davis
    Aug 31, 2005
Loading...

Share This Page