chiark / gitweb /
can normalise prices but it is wrong
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 17 May 2009 22:53:54 +0000 (23:53 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sun, 17 May 2009 22:53:54 +0000 (23:53 +0100)
ypp-restock-rum

index 7587a3c79bf1d0ec7c062219c26126d617c19129..98910ebc63581b6cb00d24fa436c75ffc9631d00 100755 (executable)
@@ -26,7 +26,7 @@ In price, missing entries mean the commodity is unavailable.
 END
 ;
 
 END
 ;
 
-our (@vals) = qw(swill grog fine shot);
+our (@kinds) = qw(swill grog fine shot);
 our (@proofs) = qw(40 60 100);
 
 sub parse_info ($$$\@$) {
 our (@proofs) = qw(40 60 100);
 
 sub parse_info ($$$\@$) {
@@ -58,7 +58,7 @@ sub parse_info ($$$\@$) {
     my $i=0;
     foreach $_ (@v) {
        $_ = $default if !length;
     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++;
     }
        $_= $1;
        $i++;
     }
@@ -89,9 +89,34 @@ sub badusage ($) {
     exit 16;
 }
 
     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;
 
 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];
     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) {
        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";
     }
 }
 
     } else {
        die "no rum available ?\n";
     }
 }
 
-
+parse_args();
+use Data::Dumper;
+print Dumper({ Have => \@haves, Want => \@want, Price => \@price });
+compute_cheapest_rum();
 
 __DATA__
 
 
 __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;
 our ($best);
 
 our $have_proof;