Professional Web Applications Themes

Help needed: urgent - PERL Beginners

================================================== ======================== 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 ------------------------------------------------- ================================================== ============== The following code works out well for this $sth->bind_columns (\$Student, \$subjectCode, \$marks); # chuck all the data into an array my ora_data; ...

  1. #1

    Default Help needed: urgent

    ================================================== ========================
    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
    -------------------------------------------------
    ================================================== ==============
    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/
    Rohit Guest

  2. #2

    Default Re: Help needed: urgent

    > ================================================== ======================== 

    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>
     

    There is always another way. Better is always debatable....

    Some Lite reading:

    perldoc perllol
    perldoc perldsc
    perldoc perlreftut
    perldoc perlref

    http://danconia.org
    Wiggins Guest

  3. #3

    Default Re: Help needed: urgent

    Always group reply so that others can help and be helped and to avoid
    getting (accidentally) ignored.
     
    ----------------------------------------------------------------------------------------------- 

    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

    ------------------------------------------------------------------------------------------------- 
    > >[/ref]
    > ================================================== ======================== 
    > >[/ref]
    > ================================================== ============== 
    > >[/ref]
    > ================================================== =========== 
    > > coded 
    > >
    > > 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:
    > > org
    > > For additional commands, e-mail:
    > > org
    > > <http://learn.perl.org/>
    > > <http://learn.perl.org/first-response>
    > >
    > >
    > >[/ref]
    >
    > __________________________________________________ ______________________
    > Yahoo! India Matrimony: Find your partner online.[/ref]
    http://yahoo.shaadi.com/india-matrimony/ 


    Wiggins Guest

Similar Threads

  1. urgent help needed!!!
    By Joe Science in forum Macromedia ColdFusion
    Replies: 0
    Last Post: April 7th, 12:10 AM
  2. Urgent Help needed with Xtra's
    By stacyp in forum Macromedia Director Basics
    Replies: 11
    Last Post: October 18th, 11:34 AM
  3. URGENT HELP NEEDED
    By Lauren_Shadle@adobeforums.com in forum Adobe Illustrator Windows
    Replies: 9
    Last Post: August 26th, 07:29 AM
  4. Urgent help Needed
    By Anantha Narayanan Vijayaraghavan in forum ASP.NET Data Grid Control
    Replies: 5
    Last Post: December 18th, 04:28 PM
  5. Urgent help needed,please
    By Bobby H in forum Macromedia Director Basics
    Replies: 2
    Last Post: October 15th, 06:56 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139