Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Perl help with html cgi table

by vkknava (Novice)
on May 17, 2015 at 09:59 UTC ( [id://1126884]=perlquestion: print w/replies, xml ) Need Help??

vkknava has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a file like below that contains datewise value for every hour for 2 locations. I need to add the value for the same hours for ServerA and ServerB. Using Hashes, I am able to add the server value per hour. But cannot pass it to a table. I am selecting "Loc" via a dropdown to use a switch to select the data.

Input File:

Loc Date Hour Server Val USA 1-Sep 00 ServerA 10 USA 1-Sep 00 ServerB 11 USA 1-Sep 01 ServerA 09 USA 1-Sep 01 ServerB 10 USA 1-Sep 02 ServerA 07 USA 1-Sep 02 ServerB 08 USA 2-Sep 00 ServerA 05 USA 2-Sep 00 ServerB 06 USA 2-Sep 01 ServerA 07 USA 2-Sep 01 ServerB 07 USA 2-Sep 02 ServerA 05 USA 2-Sep 02 ServerB 06 EUROPE 1-Sep 00 ServerA 10 EUROPE 1-Sep 00 ServerB 11 EUROPE 1-Sep 01 ServerA 09 EUROPE 1-Sep 01 ServerB 10 EUROPE 1-Sep 02 ServerA 07 EUROPE 1-Sep 02 ServerB 08 EUROPE 2-Sep 00 ServerA 05 EUROPE 2-Sep 00 ServerB 06 EUROPE 2-Sep 01 ServerA 07 EUROPE 2-Sep 01 ServerB 07 EUROPE 2-Sep 02 ServerA 05 EUROPE 2-Sep 02 ServerB 06

OUTPUT in HTML Table

Hours/Date 00 01 02 03 04 05 06 07 08 09 + 10 11 12 13 14 15 16 17 18 19 20 + 21 22 23 Sep-1 21 19 15 Sep-2 11 14 11

Code is as below

while(<DATA>) { ($keys, $date, $token, $server, $hour, $peak)= split; @th = $date; #$res{$key}{$server}->{REGION}++; #$res{$key}{$user}->{QUANTITY}++; if ($keys eq $filename){ switch($filename){ case "USA" {$res{$keys}{$date}->{DATE};$res{DATE}{ +$hour}->{PEAK}+=$peak;$flag=1} case "EUROPE" {$res{$date}{$hour}->{PEAK}+=$peak;$ +flag=1} else { print "previous case not true" } } } } if ($flag){ pgm_fil(); } my $tablecontent; sub pgm_fil{ my $q = new CGI; $q->header; $q->start_html(); print "<table border=1>"; my $tablecontent=[$q->th(['Date/Hours', '01', '02', '03', +'04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15 +', '16', '17', '18', '19', '20', '21', '22', '23'])]; print "<tr><td>Date/Hours</td>"; foreach $datea(@th){ print "<td>$datea</td>"; } foreach my $key (sort {$a<=>$b} keys %res){ foreach my $user (keys %{$res{$key}}){ foreach my $user1 (keys %{$res{$key}{$user}}){ my @hashval = $res{$key}{$user}{$user1}; foreach my $hashval( @hashval){ print "<tr><td>$user</td><td>$hashval[$_]</td></tr +>"; push @$tablecontent,$q->td([$date,$hashval[$_] ]); } } } } print "</tr></table>"; print $q->table( { border => 1, -width => '50%', -align=>' +left'}, $q->Tr( $tablecontent), ); }

Replies are listed 'Best First'.
Re: Perl help with html cgi table
by hippo (Bishop) on May 17, 2015 at 12:08 UTC

    The RHS of

                        my @hashval = $res{$key}{$user}{$user1};

    doesn't look much like an array to me. Perhaps this is (one place) where you are going wrong? It is however rather hard to tell given the overall poor shape which this code is in. If you tidy it up you might be better situated to do some debugging.

    • Run it through perltidy - the inconsistent indenting is just asking for trouble.
    • Lose the switch and case statements and replace them with something better supported.
    • Choose meaningful variable names: @hashval says very little about its purpose.
    • Use comments to explain what is going on and why.
    • Use strict.

    Once you've done that and have posted some sample code which compiles (the OP sample does not) we stand a better chance of helping you.

      Hi,

      Thanks for the response. I am beginner, so very little knowledge of the advanced stuffs to reduce the coding complexity. May be the below code will give a better picture.

      my $filename = param('textcontent'); print "<br><br><br>"; print "The Location you have selected is: $filename"; print "<br><br><br>"; my $k; my $i=0;my $file;my @argv;my %res;my $key;my $date, my +$token, my $server, my $hour, my $peak;my $flag = 0;my $tablecontent; +my @th; open(DATA, "../cgi-bin/lic.txt") || die("Could not open file!" +); while(<DATA>) { ($key, $date, $token, $server, $hour, $peak)= split; if ($key eq $filename){ switch($filename){ case "EMEA" {$res{$date}{$hour}->{PEAK}+=$peak;$fl +ag=1} case "APAC" {$res{$date}{$hour}->{PEAK}+=$peak;$fl +ag=1} else { print "previous case not true" } } } } if ($flag){ pgm_fil(); } sub pgm_fil{ my $q = new CGI; $q->header; $q->start_html(); my $tablecontent=[$q->th(['Date/Hours','00', '01', '02', ' +03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14' +, '15', '16', '17', '18', '19', '20', '21', '22', '23'])]; foreach my $key (sort {$a<=>$b} keys %res){ foreach my $user (keys %{$res{$key}}){ foreach my $user1 (keys %{$res{$key}{$user}}){ push @$tablecontent,$q->td([$key,$res{$key}{$u +ser}{$user1}]); } } } print $q->table( { border => 1, -width => '50%', -align=>' +left'}, $q->Tr( $tablecontent), ); }

        Even though the indenting is much improved in this second example, I have to tell you that it still doesn't compile. Here's what I see with v5.14.3:

        $ perl -cw 1126889.pl Unquoted string "case" may clash with future reserved word at 1126889. +pl line 12. String found where operator expected at 1126889.pl line 12, near "case + "EMEA"" (Do you need to predeclare case?) Unquoted string "case" may clash with future reserved word at 1126889. +pl line 13. String found where operator expected at 1126889.pl line 13, near "case + "APAC"" (Do you need to predeclare case?) "my" variable $q masks earlier declaration in same statement at 112688 +9.pl line 23. "my" variable $q masks earlier declaration in same scope at 1126889.pl + line 24. "my" variable $tablecontent masks earlier declaration in same scope at + 1126889.pl line 25. syntax error at 1126889.pl line 11, near "){" syntax error at 1126889.pl line 12, near "}->" syntax error at 1126889.pl line 12, near "+=" syntax error at 1126889.pl line 12, near "1}" syntax error at 1126889.pl line 13, near "1}" syntax error at 1126889.pl line 36, near "}" 1126889.pl had compilation errors.

        Start by fixing the syntax errors and (as already suggested above) replacing select and case with something else. As you say you are a beginner try using something simple and intuitive such as

        if ('EMEA' eq $filename || 'APAC' eq $filename) { $res{$date}{$hour}->{PEAK} += $peak; $flag = 1; } else { print "previous case not true"; }

        instead. Note that this is not to suggest that $res{$date}{$hour}->{PEAK} += $peak; is the correct statement at that point but that's one step farther down the line. Try compiling the code samples before you post them. If you can post code which compiles then we'll at least have some idea where you are headed.

Re: Perl help with html cgi table
by kcott (Archbishop) on May 17, 2015 at 13:10 UTC

    G'day vkknava,

    The first thing you need to do is read Perl Introduction for Beginners.

    Now that you've (hopefully) read that, let's go through your code line-by-line.

    while(<DATA>)
    There's no __DATA__ token. Perhaps you're using the "Input File" you posted.
    ($keys, $date, $token, $server, $hour, $peak)= split;
    That doesn't match your "Input File". 3rd column is "Hour" vs. 5th field is $hour. "Input File" has 5 fields; you're splitting $_ into 6 fields.
    @th = $date;
    You probably want: push @th, $date;
    if ($keys eq $filename)
    You don't initialise $filename anywhere in your code. Perhaps you meant: if (! defined $keys)
    switch ... case ... case ... else
    What language is this? You appear to be just inventing new syntax!
    $res{$keys}{$date}->{DATE}
    $res{DATE}{$hour}->{PEAK}
    $res{$date}{$hour}->{PEAK}
    You haven't defined %res anywhere in your code. Why the "->" between the 2nd and 3rd keys? You don't have "->" between the 1st and 2nd keys. Did you think it was doing something special in that position?

    I'm going to stop there. You have problems on every single line of code so far. Fix what I've pointed out. Fix the problems Perl tells you about (from the strict and warnings pragmata you'll be adding to your code). You'll also want to move that subroutine definition out of the while loop!

    If you need further help, please ensure you have read, and followed, the guidelines in "How do I post a question effectively?" before posting.

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1126884]
Approved by shmem
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-04-19 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found