This is some of the uglyest code I've seen posted on PerlMonks! There is absolutly no need for any of the gotos that are scattered all through the code!

The following does what you seem to want to do without the gotos and without the troublesome format stuff:

#!/usr/bin/perl -w use strict; use warnings; print "Packaged Goods Organizer\n========================\n"; my %val; my $total=0; while (do {print "Barcode : "; $_ = <DATA>}) { chomp; last if ($_ == "0" || ! length); if (($_ >= 10_000_000_000) || ($_ < 1_000_000_000)|| /[^0-9]/){ print "Bad input (numbers only and between 1000000000 and 9999 +999999)\n"; next; } my $code = $_; while (do {print "Price : "; $_ = <DATA>}) { chomp; if (/[^0-9\.]/) { print "Numbers only\n"; next; } $val{$code}{"Price"}= $_; last; } while (do {print "Quantity : "; $_ = <DATA>}) { chomp; if (/[^0-9]/) { print "Numbers only\n"; next; } $val{$code}{"Quantity"}= $_; last; } }; print "\n\n\t\tGoods in Stock\n\t\t==============\nBarcode Price +Quantity Value\n-----------------------------------\n"; for (sort keys %val){ my $value = $val{$_}{"Price"} * $val{$_}{"Quantity"}; printf "%10d%8.2f%9d%8.2f\n", $_, $val{$_}{"Price"}, $val{$_}{"Quantity"}, $value; $total += $value; } printf "-----\nTotal value in stock \$%.2f\n", $total; __DATA__ 1231231231 21 2 1231231232 44 2 0

Prints:

Packaged Goods Organizer ======================== Barcode : Price : Quantity : Barcode : Price : Quantity : + Barcode : Goods in Stock ============== Barcode Price Quantity Value ----------------------------------- 1231231231 21.00 2 42.00 1231231232 44.00 2 88.00 ----- Total value in stock $130.00

Note that the <DATA> uses should change to <> to get the data from stdin per your original code. I was too lazy to type the values in so opted for __DATA__ doing the work for me.


DWIM is Perl's answer to Gödel

In reply to Re: using format twice by GrandFather
in thread using format twice by zer

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.