Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > array of hashes: finding hash with min value for key

Reply
Thread Tools

array of hashes: finding hash with min value for key

 
 
Patrick Cotner
Guest
Posts: n/a
 
      09-03-2007
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", wed => 840.64, :apr => 0.2474 }
sections << { :type => "Transfers", 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/.

 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      09-03-2007
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", wed => 840.64, :apr => 0.2474 }
> sections << { :type => "Transfers", 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)

 
Reply With Quote
 
 
 
 
Stefan Rusterholz
Guest
Posts: n/a
 
      09-03-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", wed => 840.64, :apr => 0.2474 }
> sections << { :type => "Transfers", 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/.

 
Reply With Quote
 
Patrick Cotner
Guest
Posts: n/a
 
      09-04-2007
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", wed => 840.64, :apr => 0.2474 }
>> sections << { :type => "Transfers", 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/.

 
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
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
hash key to var name of value hash key value Une bévue Ruby 5 08-10-2006 04:05 PM
finding Hash subsets based on key value ee Ruby 11 06-15-2005 03:42 PM
sort multi-key hash by value and print out with key value pairs Antonio Quinonez Perl Misc 2 08-14-2003 10:56 PM



Advertisments