array of hashes (finding value)

Discussion in 'Ruby' started by Nate Leavitt, Jul 14, 2008.

  1. Nate Leavitt

    Nate Leavitt Guest

    I'm just looking for other possible solutions. Sorry, this is kinda a
    noob question.

    Currently I loop through the array and check the value of each hash key
    to find values. Is that the only and/or best way to find matches?

    example:

    @invoices = [{:id => 1, :first_name => 'nate'}, {:id => 2, :fist_name =>
    'greg'}, {:id => 3, :first_name =>}]

    @invoices.each do |invoice|
    if invoice[:id] == params[:contact_id]
    bla... bla...
    end
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Nate Leavitt, Jul 14, 2008
    #1
    1. Advertising

  2. Nate Leavitt

    Glen Holcomb Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Mon, Jul 14, 2008 at 3:22 PM, Nate Leavitt <> wrote:

    > I'm just looking for other possible solutions. Sorry, this is kinda a
    > noob question.
    >
    > Currently I loop through the array and check the value of each hash key
    > to find values. Is that the only and/or best way to find matches?
    >
    > example:
    >
    > @invoices = [{:id => 1, :first_name => 'nate'}, {:id => 2, :fist_name =>
    > 'greg'}, {:id => 3, :first_name =>}]
    >
    > @invoices.each do |invoice|
    > if invoice[:id] == params[:contact_id]
    > bla... bla...
    > end
    > end
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    With the construct you have there it would be faster to just store the names
    in an array at the index corresponding to :id. Although if this is coming
    out of Rails that probably isn't a viable option. If you are stuck with
    that construct then what you have should work fine. There are probably
    faster/more efficient ways though.

    --
    "Hey brother Christian with your high and mighty errand, Your actions speak
    so loud, I can't hear a word you're saying."

    -Greg Graffin (Bad Religion)
     
    Glen Holcomb, Jul 14, 2008
    #2
    1. Advertising

  3. Nate Leavitt

    Nate Leavitt Guest

    Glen Holcomb wrote:
    > On Mon, Jul 14, 2008 at 3:22 PM, Nate Leavitt <>
    > wrote:
    >
    >>
    >> @invoices.each do |invoice|
    >> if invoice[:id] == params[:contact_id]
    >> bla... bla...
    >> end
    >> end
    >> --
    >> Posted via http://www.ruby-forum.com/.
    >>
    >>

    > With the construct you have there it would be faster to just store the
    > names
    > in an array at the index corresponding to :id. Although if this is
    > coming
    > out of Rails that probably isn't a viable option. If you are stuck with
    > that construct then what you have should work fine. There are probably
    > faster/more efficient ways though.
    >
    > --
    > "Hey brother Christian with your high and mighty errand, Your actions
    > speak
    > so loud, I can't hear a word you're saying."
    >
    > -Greg Graffin (Bad Religion)



    Thanks for the reply. Yes, I am working in rails :) It just seems
    kinda tedious to keep iterating over all these collections. Just
    wondering if there is a more efficient way.
    --
    Posted via http://www.ruby-forum.com/.
     
    Nate Leavitt, Jul 14, 2008
    #3
  4. On Mon, Jul 14, 2008 at 2:37 PM, Nate Leavitt <> wrote:
    >>>
    >>> @invoices.each do |invoice|
    >>> if invoice[:id] == params[:contact_id]
    >>> bla... bla...
    >>> end

    >
    > Thanks for the reply. Yes, I am working in rails :) It just seems
    > kinda tedious to keep iterating over all these collections. Just
    > wondering if there is a more efficient way.


    Where did @invoices come from in the first place? If it came from the
    database, you are probably doing things in your loop that you should
    be doing in the sql query (e.g. Invoice.find(params[:contact_id])
    rather than your if guard in the loop)

    martin
     
    Martin DeMello, Jul 14, 2008
    #4
  5. Nate Leavitt wrote:
    > Glen Holcomb wrote:


    >> in an array at the index corresponding to :id. Although if this is
    >> coming
    >> out of Rails that probably isn't a viable option. If you are stuck with
    >> that construct then what you have should work fine. There are probably
    >> faster/more efficient ways though.


    >
    >
    > Thanks for the reply. Yes, I am working in rails :) It just seems
    > kinda tedious to keep iterating over all these collections. Just
    > wondering if there is a more efficient way.


    invoice_set=@invoices.select{|invoice|invoice[:id]==params[:contact_id]}

    invoice_set.each do |invoice|
    #stuff with invoice
    end

    Doubt if it is less work but it feels snappier somehow.

    Regards,

    Siep
    --
    Posted via http://www.ruby-forum.com/.
     
    Siep Korteling, Jul 14, 2008
    #5
  6. Nate Leavitt

    Nate Leavitt Guest

    Martin DeMello wrote:
    > On Mon, Jul 14, 2008 at 2:37 PM, Nate Leavitt <>
    > wrote:
    >>>>
    >>>> @invoices.each do |invoice|
    >>>> if invoice[:id] == params[:contact_id]
    >>>> bla... bla...
    >>>> end

    >>
    >> Thanks for the reply. Yes, I am working in rails :) It just seems
    >> kinda tedious to keep iterating over all these collections. Just
    >> wondering if there is a more efficient way.

    >
    > Where did @invoices come from in the first place? If it came from the
    > database, you are probably doing things in your loop that you should
    > be doing in the sql query (e.g. Invoice.find(params[:contact_id])
    > rather than your if guard in the loop)
    >
    > martin


    Yeah... sorry. I actually get the collection through an xml-rpc call :)
    which returns the array of hashes.
    --
    Posted via http://www.ruby-forum.com/.
     
    Nate Leavitt, Jul 14, 2008
    #6
    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. kazaam
    Replies:
    12
    Views:
    278
    Matthias Wächter
    Sep 13, 2007
  2. Patrick Cotner
    Replies:
    3
    Views:
    105
    Patrick Cotner
    Sep 4, 2007
  3. Matt Brooks
    Replies:
    16
    Views:
    228
    Matt Brooks
    Sep 16, 2009
  4. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    214
  5. mrstevegross
    Replies:
    3
    Views:
    138
    Tad J McClellan
    Nov 11, 2008
Loading...

Share This Page