Hmm, do you just want it shorter and still produce the wrong total, or shorter and produce the correct total as well?

# Let's see how much money I would have if I just won the # lottery and found a 2 pound coin in the couch cushions... How many 5 pence pieces do you have? 0 How many 10 pence pieces do you have? 0 How many 20 pence pieces do you have? 0 How many 50 pence pieces do you have? 0 How many £1 coins do you have? 1000000000 How many £2 coins do you have? 1 How many £5 notes do you have? 0 How many £10 notes do you have? 0 How many £20 notes do you have? 0 How many £50 notes do you have? 0 You have £2. # Hmm, guess I won't spend my £2 on a lottery ticket then...

strict would have helped you out there... In any case...

#!/usr/bin/perl -w use strict; use warnings; use List::Util qw( sum ); printf( "You have £%.02f\n", sum ( ( map { ask( "$_ pence pieces", $_/100 ) } ( 5, 10, 20, 50 ) ), ( map { ask( "£$_ coins", $_ ) } ( 1, 2 ) ), ( map { ask( "£$_ notes", $_ ) } ( 5, 10, 20, 50 ) ), ) ); sub ask { print "How many $_[0] do you have? " ; <STDIN> * $_[1] }

www.jasonkohles.com
We're not surrounded, we're in a target-rich environment!

In reply to Re: Can someone help me make this script more efficient by jasonk
in thread Can someone help me make this script more efficient by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.