I still don't understand what your input data might look like, but is this closer to your output example?
#!/usr/bin/perl -w
use strict;
use warnings;
my ( $county, $year_x, $yr_tot, $production );
my $result = &make_table;
print $result;
sub make_table {
open YEARLY, '>', \$production;
print YEARLY " 2001 2002 2003 2004 \n";
while ( <DATA> ) {
( $county, $year_x, $yr_tot ) = split ' ', $_;
if ( $year_x eq "1" || "2" || "3" || "4" ) {
$year_x += $yr_tot;
write YEARLY;
}
}
return $production;
}
format YEARLY =
@<<<<<<<< @<<<<
$county, $year_x
.
close YEARLY;
__DATA__
McMahon 1 9000
McHardy 2 8000
Moonlit 3 7000
Murphie 4 6000
Returns:
2001 2002 2003 2004
McMahon 9001
McHardy 8002
Moonlit 7003
Murphie 6004
No writing to a disk file until you need to...
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.