Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to combine hash with formatted output.
Here is my script:
%auto = (GM => 'Caprice',Ford => 'Pinto',Honda => 'Civic',Toyota => 'T +acoma'); foreach $key (keys %auto) { print "$key $auto{$key}\n"; } format HEADER = MANUFACTER----MODEL . format FLOR = @<<<<<<< @######## $key, $value . write while (($key,$value) = each %auto);

It just outputs the data without the headers such as:
Toyota Tacoma Ford Pinto Honda Civic GM Caprice
I want it to output like this:
MANUFACTURER MODEL ----------------------- Toyota Tacoma Ford Pinto Honda Civic GM Caprice

Replies are listed 'Best First'.
Re: Format with hash
by VSarkiss (Monsignor) on May 15, 2002 at 15:03 UTC

    Begin by renaming your formats to match the currently selected output stream, which is STDOUT by default:

    format STDOUT_TOP = MANUFACTER----MODEL . format STDOUT = @<<<<<<< @######## $key, $value .
    Once that works, read perlform for information on how to define multiple formats and how to change them for the currently selected output stream. If you want to continue to use the names you have, try this:
    use English; # less line noise ;-) $FORMAT_TOP_NAME = 'HEADER'; $FORMAT_NAME = 'FLOR';

    HTH