Help required in accessing hash references.

R

Rohit S

==========================================================================
Using DBI Perl Programming I get a database o/p as
below
Student SubjectCode Marks
------------------------------------------------
A Ma 90
A Sc 89
B Ma 70
B Sc 71
B Ge 71
C Sc 73
C Ge 97
... ... ...
-------------------------------------------------
Subject code may vary to any value.
I need a report o/p in the following format and
displayed in HTML
Student Ma Sc Ge .....
------------------------------------------------
A 90 89
B 70 71 71
C 73 97
-------------------------------------------------


# chuck all the data into an array
my $students;
while (my $row = $sth->fetchrow_hashref) {
$students->{$row->{$student}}->{$row->{$subject}} = $row->{$mark};


foreach $student(sort keys %$students) {
print $student, "\t";
foreach $subject(sort keys %{$students->{$student}}) {
print "\t", $students->{$student}->{$subject};
}
print "\n";
}


================================================================
The subject field would vary
But this doesnt seem to work.I also need to put the values in an matrix.

Could anyone pls help me out in this.

Regards
Rohit
 
T

Tad McClellan

Rohit S said:
my $students;
while (my $row = $sth->fetchrow_hashref) {
$students->{$row->{$student}}->{$row->{$subject}} = $row->{$mark};


You should always enable warnings and strictures when developing Perl code!

What are the values in the $student, $subject and $mark variables?

Maybe those are supposed to be hash keys from your query rather
than variables? (eg: $students->{$row->{student}} ...)

But this doesnt seem to work.


What does it do?

What do you want it to do instead?

Have you seen the Posting Guidelines that are posted here frequently?

Could anyone pls help me out in this.


If you post a short and complete program that we can run, I am sure
that someone can solve your problem.

Without that we can only guess...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top