From: Ian Jackson Date: Sun, 17 May 2009 22:53:54 +0000 (+0100) Subject: can normalise prices but it is wrong X-Git-Tag: 1.0~21 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-test.git;a=commitdiff_plain;h=2d35f1d97444ff9c38cca026c4b5b92f567fe59d;ds=inline can normalise prices but it is wrong --- diff --git a/ypp-restock-rum b/ypp-restock-rum index 7587a3c..98910eb 100755 --- a/ypp-restock-rum +++ b/ypp-restock-rum @@ -26,7 +26,7 @@ In price, missing entries mean the commodity is unavailable. END ; -our (@vals) = qw(swill grog fine shot); +our (@kinds) = qw(swill grog fine shot); our (@proofs) = qw(40 60 100); sub parse_info ($$$\@$) { @@ -58,7 +58,7 @@ sub parse_info ($$$\@$) { my $i=0; foreach $_ (@v) { $_ = $default if !length; - m/^0*(\d+)$/ or badusage("$what: $vals[$i] \`$_': bad syntax"); + m/^0*(\d+)$/ or badusage("$what: $kinds[$i] \`$_': bad syntax"); $_= $1; $i++; } @@ -89,9 +89,34 @@ sub badusage ($) { exit 16; } -parse_args(); -use Data::Dumper; -print Dumper({ Have => \@haves, Want => \@want, Price => \@price }); +our $ff = '%6.1f'; + +sub prvff ($$\@$) { + my ($what, $format, $ary, $unit) = @_; + printf("%-40s", "$what:"); + for my $i (qw(0 1 2)) { + my $x= $ary->[$i]; + my $y= $x>0 && $x<1e5 ? sprintf $format, $x : ' x'; + printf " %-10s", $y; + } + printf " %s\n", $unit; +} + +sub pr ($\@$) { + my ($what, $ary, $unit) = @_; + prvff($what, '%4d ', @$ary, $unit); +} + +sub prf ($\@$) { + my ($what, $ary, $unit) = @_; + prvff($what, $ff, @$ary, $unit); +} + +sub pr1 ($$) { + my ($k,$v) = @_; + printf "%-20s %s\n", "$k:", $v; +} + our @norm_price; our $best; @@ -103,51 +128,26 @@ sub compute_cheapest_rum() { my $best_norm_price= 1e5; for my $i (qw(0 1 2)) { next unless $price[$i]; - $norm_price[$i] = $price[$i] * $proof[$i] / $proof[$i]; + $norm_price[$i] = $price[$i] * $proofs[$i] / $proofs[$i]; if ($norm_price[$i] <= $best_norm_price) { $best= $i; } }; prf('normalised prices', @norm_price, 'poe/fine'); if (defined $best) { - printf "best is %s\n\n", $best; + printf "best is %s\n\n", $kinds[$best]; } else { die "no rum available ?\n"; } } - +parse_args(); +use Data::Dumper; +print Dumper({ Have => \@haves, Want => \@want, Price => \@price }); +compute_cheapest_rum(); __DATA__ -our $ff = '%6.1f'; - -sub prvff ($$\%$) { - my ($what, $format, $ary, $unit) = @_; - printf("%-40s", "$what:"); - map { - my $x= $ary->{$_}; - my $y= defined $x ? sprintf $format, $x : ' x'; - printf " %-10s", $y; - } @rums; - printf " %s\n", $unit; -} - -sub pr ($\%$) { - my ($what, $ary, $unit) = @_; - prvff($what, '%4d ', %$ary, $unit); -} - -sub prf ($\%$) { - my ($what, $ary, $unit) = @_; - prvff($what, $ff, %$ary, $unit); -} - -sub pr1 ($$) { - my ($k,$v) = @_; - printf "%-20s %s\n", "$k:", $v; -} - our ($best); our $have_proof;