You need to learn about program structure and data structures. Here is an example. I recommend you don't submit it verbatim unless you understand it properly:

my $grocerylist = { Eggs => '1.99', Milk => '1.75', Cookies => '2.99', Carrots => '1.25', Eggplant => '1.89', Cereal => '3.25', Gum => '0.75', Juice => '1.99' }; my $cart = { items => {}, total => 0, cash => 0, }; print "Welcome to the Acme(tm) grocery store.\n"; print "How much do you have to spend today? "; chomp ($cart->{cash} = <STDIN>); printf "You have: \$%.2f\n", $cart->{cash}; while ( ( $cart->{cash} - $cart->{total} ) > 0) { my $action = menu(); if ( $action eq 'Q') { quit($cart); } elsif ($action eq 'Showall' ) { showall($grocerylist); } elsif ($action eq 'Cart'){ cart($cart); } elsif (exists $grocerylist->{$action}){ add_cart( $cart, $grocerylist, $action ); } else { print "\n*****Don't know how to do $action\n\n"; } } sub add_cart { my ( $cart, $grocerylist, $item ) = @_; if ( ($cart->{total} + $grocerylist->{$item}) > $cart->{cash} ) { printf "\nSorry you can't afford '%s' @ \$%.2f as you only hav +e \$%.2f\n", $item, $grocerylist->{$item}, $cart->{cash}-$cart->{total} +; } else { $cart->{items}->{$item}++; $cart->{total} += $grocerylist->{$item}; print "\nAdded $item to cart OK\n"; } } sub menu { print qq! Enter: item name, 'cart' to view your current purchases, 'showall' to show all available items. 'q' to quit and display purchases. Action: !; chomp (my $action = <STDIN>); $action = ucfirst(lc $action); return $action; } sub quit { my $cart = shift; checkout($cart); } sub cart { my $cart = shift; print "This is Cart so far:\n\n"; printf("%-15s %s\n" .('-'x25) ."\n", "Item", "Quantity"); printf ("%-15s %s\n", $_, $cart->{items}->{$_}) for keys %{$cart-> +{items}}; printf " Spent \$%.2f Budget: \$%.2f Remaining: \$%.2f\n", $cart->{total}, $cart->{cash}, $cart->{cash} - $cart->{total}; } sub showall { my $grocerylist = shift; printf("%-15s %s\n" . ('-'x25) ."\n", 'Item', 'Price'); printf("%-15s %s\n", $_, $grocerylist->{$_}) for keys %$grocerylis +t; } sub checkout { my $cart = shift; require Data::Dumper; print Data::Dumper::Dumper($cart); exit 0; }

In reply to Re: Need help on part of homework by Anonymous Monk
in thread Need help on part of homework by gitarwmn

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.