> ==========================================================================
> Using DBI Perl Programming I get a database o/p as
> below
> Student SubjectCode Marks
> ------------------------------------------------
> A 1 90
> A 2 89
> B 1 70
> B 2 71
> B 3 71
> C 2 73
> C 3 97
> -------------------------------------------------
> Subject code may vary to any value.
> I need a report o/p in the following format and
> displayed in HTML
> Student 1 2 3 4
> ------------------------------------------------
> A 90 89
> B 70 71 71
> C 73 97
> -------------------------------------------------
> ================================================================
I am not sure why you chose an array as your top level structure.
Assuming each student is unique, and each subject is unique, you can use
a hash of students, with the values of that being a hash of subjects,
with the value being the mark. This eliminates the need for all the
index munging. For me it would look something like:
use strict;
use warnings;
my $sth = [
{ 'student' => 'A', 'subject' => '1', 'mark' =>'90' },
{ 'student' => 'A', 'subject' => '2', 'mark' =>'89' },
{ 'student' => 'B', 'subject' => '1', 'mark' =>'70' },
{ 'student' => 'B', 'subject' => '2', 'mark' =>'71' },
{ 'student' => 'B', 'subject' => '3', 'mark' =>'71' },
{ 'student' => 'C', 'subject' => '2', 'mark' =>'73' },
{ 'student' => 'C', 'subject' => '3', 'mark' =>'97' },
];
my $students;
foreach my $row (@$sth) {
#while (my $row = $sth->fetchrow_hashref) {
$students->{$row->{'student'}}->{$row->{'subject'}} = $row->{'mark'};
}
foreach my $student (sort keys %$students) {
print $student, "\t";
foreach my $subject (sort keys %{$students->{$student}}) {
print "\t", $students->{$student}->{$subject};
}
print "\n";
}
I have simulated your select above with the data you provided, switch
the C<foreach> to the commented C<while> to have it use the actual
statement handle.
You can use C<printf> or formats to get the columns to line up right.
<snip code>
> =============================================================
> Is there any other way in which this could be coded
> efficiently.
> Regards
> Rohit
There is always another way. Better is always debatable....
Some Lite reading:
perldoc perllol
perldoc perldsc
perldoc perlreftut
perldoc perlref
http://danconia.org
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>