array of hashes: finding hash with min value for key

Discussion in 'Ruby' started by Patrick Cotner, Sep 3, 2007.

  1. I'm trying to write a proggy to help me figure out how long it will take
    to pay off a credit card based on a payment value.
    The card can have multiple sections with different owed amounts and
    aprs.
    Since the credit card companies pay off lower interest sections prior to
    higher, I need to be able to determine which section has lowest apr. For
    example

    <code>
    sections << { :type => "Purchases", :eek:wed => 840.64, :apr => 0.2474 }
    sections << { :type => "Transfers", :eek:wed => 3453.45, :apr => 0.1774 }

    </code>

    I've set up a loop to iterate through each section and apply a finance
    charge to the owed amounts. I have a variable with my payment amount.
    After applying the finance charge, i need to see if section[:apr] is the
    lowest of all sections, in order to apply the payment to that section
    only.
    How would this be done?

    -Patrick
    --
    Posted via http://www.ruby-forum.com/.
    Patrick Cotner, Sep 3, 2007
    #1
    1. Advertising

  2. Patrick Cotner

    Guest

    Hi --

    On Tue, 4 Sep 2007, Patrick Cotner wrote:

    > I'm trying to write a proggy to help me figure out how long it will take
    > to pay off a credit card based on a payment value.
    > The card can have multiple sections with different owed amounts and
    > aprs.
    > Since the credit card companies pay off lower interest sections prior to
    > higher, I need to be able to determine which section has lowest apr. For
    > example
    >
    > <code>
    > sections << { :type => "Purchases", :eek:wed => 840.64, :apr => 0.2474 }
    > sections << { :type => "Transfers", :eek:wed => 3453.45, :apr => 0.1774 }
    >
    > </code>
    >
    > I've set up a loop to iterate through each section and apply a finance
    > charge to the owed amounts. I have a variable with my payment amount.
    > After applying the finance charge, i need to see if section[:apr] is the
    > lowest of all sections, in order to apply the payment to that section
    > only.
    > How would this be done?


    You could say:

    lower = sections.find {|s| s[:apr] < section[:apr] }

    and if that's non-nil, you've found a lower one.


    David

    --
    * Books:
    RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
    RUBY FOR RAILS (http://www.manning.com/black)
    * Ruby/Rails training
    & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
    , Sep 3, 2007
    #2
    1. Advertising

  3. Patrick Cotner wrote:
    > I'm trying to write a proggy to help me figure out how long it will take
    > to pay off a credit card based on a payment value.
    > The card can have multiple sections with different owed amounts and
    > aprs.
    > Since the credit card companies pay off lower interest sections prior to
    > higher, I need to be able to determine which section has lowest apr. For
    > example
    >
    > <code>
    > sections << { :type => "Purchases", :eek:wed => 840.64, :apr => 0.2474 }
    > sections << { :type => "Transfers", :eek:wed => 3453.45, :apr => 0.1774 }
    >
    > </code>
    >
    > I've set up a loop to iterate through each section and apply a finance
    > charge to the owed amounts. I have a variable with my payment amount.
    > After applying the finance charge, i need to see if section[:apr] is the
    > lowest of all sections, in order to apply the payment to that section
    > only.
    > How would this be done?
    >
    > -Patrick


    ri Enumerable#min
    sections.min { |a,b| a[:apr] <=> b[:apr] }

    Regards
    Stefan
    --
    Posted via http://www.ruby-forum.com/.
    Stefan Rusterholz, Sep 3, 2007
    #3
  4. [solved]array of hashes: finding hash with min value for key

    Stefan Rusterholz wrote:
    > Patrick Cotner wrote:
    >> I'm trying to write a proggy to help me figure out how long it will take
    >> to pay off a credit card based on a payment value.
    >> The card can have multiple sections with different owed amounts and
    >> aprs.
    >> Since the credit card companies pay off lower interest sections prior to
    >> higher, I need to be able to determine which section has lowest apr. For
    >> example
    >>
    >> <code>
    >> sections << { :type => "Purchases", :eek:wed => 840.64, :apr => 0.2474 }
    >> sections << { :type => "Transfers", :eek:wed => 3453.45, :apr => 0.1774 }
    >>
    >> </code>
    >>
    >> I've set up a loop to iterate through each section and apply a finance
    >> charge to the owed amounts. I have a variable with my payment amount.
    >> After applying the finance charge, i need to see if section[:apr] is the
    >> lowest of all sections, in order to apply the payment to that section
    >> only.
    >> How would this be done?
    >>
    >> -Patrick

    >
    > ri Enumerable#min
    > sections.min { |a,b| a[:apr] <=> b[:apr] }
    >
    > Regards
    > Stefan


    Thanks, David, but I went with Stephan's example.
    I posted the (ugly) finished code over here:
    http://www.ruby-forum.com/topic/123523
    Thanks, Stephan!
    --
    Posted via http://www.ruby-forum.com/.
    Patrick Cotner, Sep 4, 2007
    #4
    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. Une bévue
    Replies:
    5
    Views:
    146
    Une bévue
    Aug 10, 2006
  2. kazaam
    Replies:
    12
    Views:
    267
    Matthias Wächter
    Sep 13, 2007
  3. Matt Brooks
    Replies:
    16
    Views:
    225
    Matt Brooks
    Sep 16, 2009
  4. Scott  Gilpin
    Replies:
    2
    Views:
    216
  5. 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:
    207
Loading...

Share This Page