Re: Help needed: urgent
Always group reply so that others can help and be helped and to avoid getting (accidentally) ignored. --- But I need the output as an array like @db_results which would be sent to a HTML template. The @db_results would look like db_results[0] = A 90 89 0 0 db_results[1] = B 70 71 71 0 db_results[2] = C 0 73 97 0 In this case you need to make an attempt. I have shown you an easy way to build a structure that will make manipulating the data simple. You need to put the effort into taking it the next step, if you fail, show us what you tried and what the errors are, we are not here to write it for you. perldoc -f push perldoc -f sprintf Should give you a hint. http://danconia.org - Regards Rohit Satabhai --- Wiggins d Anconia [EMAIL PROTECTED] wrote: == Using DBI Perl Programming I get a database o/p as below Student SubjectCodeMarks A 190 A 289 B 170 B 271 B 371 C 273 C 397 - 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 Cforeach to the commented Cwhile to have it use the actual statement handle. You can use Cprintf 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 Yahoo! India Matrimony: Find your partner online. http://yahoo.shaadi.com/india-matrimony/ X-UIDL: Q`!!QdS!aj8!D3'#! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Help needed: urgent
== Using DBI Perl Programming I get a database o/p as below Student SubjectCodeMarks A 190 A 289 B 170 B 271 B 371 C 273 C 397 - 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 - The following code works out well for this $sth-bind_columns (\$Student, \$subjectCode, \$marks); # chuck all the data into an array my @ora_data; my $index=0; my $count=1; while ($sth-fetch) #fetching the first row { #If its the first record assign the student id to the first row if ($count == 1) { $ora_data[$index]{'STUDENT'} = $student; $count++; } #Compare the student id of the array with the row fetched from the database #if its not equal then create the next row in the array and assign the student to the row if ( $ora_data[$index]{'STUDENT'} ne $student) { $index++; $ora_data[$index]{'STUDENT'} = $student; } #If the studentid in the array is equal to the row fetched from the database i.e sth-fetch #Compare the subject code and assign the appropriate value of marks to the studentid. if ($ora_data[$index]{'STUDENT'} eq $student) { if ($subjectCode == 1) { $ora_data[$index]{'MARK1'} = $marks; } elsif ($subjectCode == 2) { $ora_data[$index]{'MARK2'} = $marks; } elsif ($subjectCode == 3) { $ora_data[$index]{'MARK3'} = $marks; } elsif ($subjectCode == 512) { $ora_data[$index]{'MARK4'} = $marks; } } } # close statement handler and pass the results back. $sth-finish; return @ora_data; = Is there any other way in which this could be coded efficiently. Regards Rohit - Yahoo! India Matrimony: Find your partner online. http://yahoo.shaadi.com/india-matrimony/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Help needed: urgent
== Using DBI Perl Programming I get a database o/p as below Student SubjectCodeMarks A 190 A 289 B 170 B 271 B 371 C 273 C 397 - 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 Cforeach to the commented Cwhile to have it use the actual statement handle. You can use Cprintf 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