zer--

You'll want to read up the "perlform" man page and see how to use multiple formats. There are several ways listed, but my favorite is shown below. (I got really bored and hacked on your program a little)

#!/usr/bin/perl -w use strict; use warnings; use FileHandle; my (%val, $total); $total=0; format DETAIL = @<<<<<<<<<<<<<$@<<<<<<<<<$@<<<<<$@ $_, $val{$_}{"Price"}, $val{$_}{"Quantity"}, $val{$_}{"Price"} * $val{ +$_}{"Quant ity"} . format SUMMARY= <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<$@ "-----" Total value good in stock>>>>>>>$@ $total . print "Packaged Goods Organizer\n" . "========================\n"; # Collect the data ENTRY: while (1) { BAR: while (1) { print "Barcode : "; $_=<>; chomp; last ENTRY if ($_ == "0"); last BAR unless ($_ >= 10000000000) || ($_ < 100000000 +0) || /[^0 -9]/; print "Bad input (numbers only and between 1000000000 +and 999999 9999)\n"; } Price: while (1) { print "Price : "; $val{$_}{"Price"}=<>; chomp($val{$_}{"Price"}); last Price unless ($val{$_}{"Price"}=~ /[^0-9\.]/); print "Numbers only\n"; } Quant: while (1) { print "Quantity : "; $val{$_}{"Quantity"}= <>; chomp($val{$_}{"Quantity"}); last Quant unless ($val{$_}{"Quantity"}=~ /[^0-9]/); print "Numbers only\n"; } } # Write the detail records print "\n\n\t\tGoods in Stock\n" . "\t\t==============\n" . "Barcode Price Quantity Value\n" . "-----------------------------------\n"; format_name STDOUT "DETAIL"; foreach (sort keys %val) { write STDOUT; $total += $val{$_}{"Price"} * $val{$_}{"Quantity"}; } # Write the summary format_name STDOUT "SUMMARY"; write STDOUT; close(STDOUT);
Specifically, I added the 'use FileHandle;' up front, renamed your formats to DETAIL and SUMMARY (and moved 'em closer to the front, where I normally put 'em in my programs). Finally, in the code, I put in the 'format_name' statements to tell STDOUT which format to use.

--roboticus


In reply to Re^3: using format twice by roboticus
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.