www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Max/Min values in an associative array

reply "TJB" <broughtj gmail.com> writes:
I am trying to find the max and min values in an associative 
array. Say I have:

double[char] bids;
bid['A'] = 37.50;
bid['B'] = 38.11;
bid['C'] = 36.12;

How can I find the max and min values. I am thinking that I need 
to use max and min functions from std.algorithm, but not sure how 
to.

Thanks!
TJB
Aug 06 2014
next sibling parent reply Justin Whear <justin economicmodeling.com> writes:
On Wed, 06 Aug 2014 17:57:54 +0000, TJB wrote:

 I am trying to find the max and min values in an associative array. Say
 I have:
 
 double[char] bids;
 bid['A'] = 37.50;
 bid['B'] = 38.11;
 bid['C'] = 36.12;
 
 How can I find the max and min values. I am thinking that I need to use
 max and min functions from std.algorithm, but not sure how to.
 
 Thanks!
 TJB
Do you just need the min and max values or do you also need the keys of those values? If the former, here's a paste: http://dpaste.dzfl.pl/0bbf31278a25
Aug 06 2014
parent "TJB" <broughtj gmail.com> writes:
Justin,

That's it!  Perfect - thanks!!
TJB

 Do you just need the min and max values or do you also need the 
 keys of
 those values?  If the former, here's a paste:
 http://dpaste.dzfl.pl/0bbf31278a25
Aug 06 2014
prev sibling next sibling parent "Martijn Pot" <martijnpot52 gmail.com> writes:
On Wednesday, 6 August 2014 at 17:57:55 UTC, TJB wrote:
 I am trying to find the max and min values in an associative 
 array. Say I have:

 double[char] bids;
 bid['A'] = 37.50;
 bid['B'] = 38.11;
 bid['C'] = 36.12;

 How can I find the max and min values. I am thinking that I 
 need to use max and min functions from std.algorithm, but not 
 sure how to.

 Thanks!
 TJB
You can extract the values into a double[] using bid.values. Then you can simply use max and min from std.algorithm.
Aug 06 2014
prev sibling next sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Wed, Aug 06, 2014 at 05:57:54PM +0000, TJB via Digitalmars-d-learn wrote:
 I am trying to find the max and min values in an associative array. Say I
 have:
 
 double[char] bids;
 bid['A'] = 37.50;
 bid['B'] = 38.11;
 bid['C'] = 36.12;
 
 How can I find the max and min values. I am thinking that I need to use max
 and min functions from std.algorithm, but not sure how to.
[...] import std.algorithm : reduce, max, min; auto highest = reduce!((a,b) => max(a,b))(-double.max, bids.byValue()); auto lowest = reduce!((a,b) => min(a,b))(double.max, bids.byValue()); T -- Designer clothes: how to cover less by paying more.
Aug 06 2014
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 6 August 2014 at 18:07:08 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
 	import std.algorithm : reduce, max, min;

 	auto highest = reduce!((a,b) => max(a,b))(-double.max, 
 bids.byValue());
 	auto lowest = reduce!((a,b) => min(a,b))(double.max, 
 bids.byValue());


 T
Take a look at Justin Whear's dpaste. Dual pred reduce FTW.
Aug 15 2014
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Fri, Aug 15, 2014 at 04:51:59PM +0000, monarch_dodra via Digitalmars-d-learn
wrote:
 On Wednesday, 6 August 2014 at 18:07:08 UTC, H. S. Teoh via
 Digitalmars-d-learn wrote:
	import std.algorithm : reduce, max, min;

	auto highest = reduce!((a,b) => max(a,b))(-double.max, bids.byValue());
	auto lowest = reduce!((a,b) => min(a,b))(double.max, bids.byValue());


T
Take a look at Justin Whear's dpaste. Dual pred reduce FTW.
Yeah I saw that. Learned something new. :-) T -- Bare foot: (n.) A device for locating thumb tacks on the floor.
Aug 15 2014
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
TJB:

 I am trying to find the max and min values in an associative 
 array. Say I have:

 double[char] bids;
 bid['A'] = 37.50;
 bid['B'] = 38.11;
 bid['C'] = 36.12;

 How can I find the max and min values. I am thinking that I 
 need to use max and min functions from std.algorithm, but not 
 sure how to.
void main() { import std.stdio, std.algorithm; double[char] bids = ['A': 37.50, 'B': 38.11, 'C': 36.12]; bids.byValue.reduce!(min, max).writeln; } Bye, bearophile
Aug 14 2014