in reply to Perl Format on arrays

Formats are named after the file handle you are writing to. For example:
sub testf { my $object = shift; my ($sname,$class,$date,$classid); foreach my $item (@{$object->{'school'}}) { $sname = $item->{'sname'}; $class = $item->{'class'}; $date = $item->{'EnrolDate'}; $classid = $item->{'classid'}; write STDOUT } format STDOUT = Student Name @<<<<<<<<<<<<<<<< $sname Class: @>>>>>>>>>>>> $class Enrolled Date: @||||||| $date Class id.: @||||||||| $classid . } my $object = {school => [ {sname => 'Asname', class => 'Aclass', EnrolDate => '10/10/66', classid => 'Perl'}, {sname => 'Bsname', class => 'Bclass', EnrolDate => '10/10/66', classid => 'Perl'}, {sname => 'Csname', class => 'Cclass', EnrolDate => '10/10/66', classid => 'Perl'} ]}; testf($object);
Gives:
Student Name Asname Class: Aclass Enrolled Date: 10/10/66 Class id.: Perl Student Name Bsname Class: Bclass Enrolled Date: 10/10/66 Class id.: Perl Student Name Csname Class: Cclass Enrolled Date: 10/10/66 Class id.: Perl
Personally I find printf simpler.

Update: Added blank line to break-up each record. Also note that the format cannot be indented, even though it is in a subroutine.

Replies are listed 'Best First'.
Re^2: Perl Format on arrays
by tospo (Hermit) on Apr 18, 2011 at 10:25 UTC
    +1 on using printf instead