Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: using format twice

by zer (Deacon)
on Apr 05, 2006 at 02:38 UTC ( [id://541258]=note: print w/replies, xml ) Need Help??


in reply to Re: using format twice
in thread using format twice

Update: i removed the close and this is what i get. And as more of an insight to the error... when i changed the second format name to something other than STDOUT it wouldnt print because it is trying to write to an unopened file handle.

#!/usr/bin/perl -w use strict; use warnings; print "Packaged Goods Organizer\n========================\n"; my %val;my $total=0; DO:{ BAR : print "Barcode : ";$_=<>;chomp;last if ($_ == "0"); if (($_ >= 10000000000)|| ($_ < 1000000000)||/[^0-9]/){ print "Bad input (numbers only and between 1000000000 and 9999 +999999)\n"; goto BAR; }else{ Price : print "Price : "; $val{$_}{"Price"}=<>; chomp($val{$_}{"Price"}); if ($val{$_}{"Price"}=~ /[^0-9\.]/){ print "Numbers only\n"; goto Price; } Quant : print "Quantity : "; $val{$_}{"Quantity"}= <>; chomp($val{$_}{"Quantity"}); if ($val{$_}{"Quantity"}=~ /[^0-9]/){ print "Numbers only\n"; goto Quant; } }goto DO; }; print "\n\n\t\tGoods in Stock\n\t\t==============\nBarcode Price +Quantity Value\n-----------------------------------\n"; foreach (sort keys %val){ format STDOUT = @<<<<<<<<<<<<<$@<<<<<<<<<$@<<<<<$@ $_, $val{$_}{"Price"}, $val{$_}{"Quantity"}, $val{$_}{"Price"} * $val{ +$_}{"Quantity"} . write STDOUT; $total += $val{$_}{"Price"} * $val{$_}{"Quantity"}; }close (STDOUT); format STDOUT = <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<$@ "-----" Total value good in stock>>>>>>>$@ $total . write STDOUT;
OUTPUT
Format STDOUT redefined at C:\Documents and Settings\\My Documents\per +ltest \Perl-1.pl line 38. Packaged Goods Organizer ======================== Barcode : 1231231231 Price : 22 Quantity : 1 Barcode : 0 Goods in Stock ============== Barcode Price Quantity Value ----------------------------------- <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<$- Total value good in stock>>>>>>>$0 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<$- Total value good in stock>>>>>>>$2

Replies are listed 'Best First'.
Re^3: using format twice
by roboticus (Chancellor) on Apr 05, 2006 at 02:48 UTC
    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

      Awsome! this works

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://541258]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found