Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Writing an opinion poll in perl.

by DigitalKitty (Parson)
on Sep 20, 2003 at 07:28 UTC ( [id://292820]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all.

I'm writing a small opinion poll using perl/cgi and it appears I need a little assistance. See below for code:

#!c:\perl\bin\perl.exe -wT use strict; use CGI qw(:standard); my $line; my %survey; print header; open( FH, ">>psy.txt") or die "Error : $!\n"; foreach my $key( param() ) { print FH param($key), "\n"; } close FH; open( FH, "<psy.txt" ) or die "Error : $!\n"; while( $line = <FH> ) { $survey{$line}++; } foreach my $key ( keys %survey ) { print table(Tr(td( { -width => "$survey{$key}", -height => "30", -bgcolor => "red" } ) ) ), br; } close FH;


The HTML form consists of 15 questions. Each question can be answered via one of four radio buttons. In an attempt to simplify the design aspects, I followed davido's advice and used unique 'id' numbers (starting with 1 and ending with 60). Question 1 is 1..4, question 2 is 5..8, etc. These are written to a small text file then placed into a hash in order to generate a small histogram (via CGI.pm). I'd like to display the output in the following manner:

  • Each form question in a table header. In a table row below it, how many responses (integer) and a graphical bar representing this quantity.


Note:This is *not* a homework assignment. It is a favor for a psychology professor.

Thanks,
-Katie.

Replies are listed 'Best First'.
Re: Writing an opinion poll in perl.
by cchampion (Curate) on Sep 20, 2003 at 10:49 UTC

    There is WWW-Poll, but it is quite buggy. I tried it, and I had to modify a few things just to be able to run the tests.

    It may be a good start if you don't want to write your own system from scratch.

    In Poll.pm, change line 281 from  undef (@maxvotes,@votes); to undef (@maxvotes); undef(@votes);

    In the demo script you have to change the starting path to "data" and it should work. After that, you're on your own.

    Good luck.

Re: Writing an opinion poll in perl.
by benn (Vicar) on Sep 20, 2003 at 10:24 UTC
    I'm sure there must be some modules lying around on CPAN to do exactly this, but if you want to roll your own...
    • work out the minimum and maximum values of the graph - foreach (keys %survey) {$max = $_ if $survey{$_} > $survey{$max}; repeat and rinse for $min} or something.
    • Do some maths to work out your scale factor,depending on how big you want your graph
    • decide how you want to do your graphics - the two most common ways are stretching a single-pixel (<img width=$value height=$size_of_a_column src='pixel.gif' >) or just printing <td>'s with coloured backgrounds - either a single td with a styled size (using padding-right or similar) or a bunch of 'unit' cells that get filled in or not as the case may be.
    • From then on it's all just print loops... :)
    HTH, Ben.
Re: Writing an opinion poll in perl.
by Jaap (Curate) on Sep 20, 2003 at 09:38 UTC
    And what is your question?
      Hi.

      I need to know how to display the data in the following format for each question on the form as well as how many 'votes' each of the four choices received:


      I think pasta is good
      Strongly agree********
      Agree**
      Disagree*************
      Strongly disagree****

      The asterisks would represent the red bar which is a graphical measure of each hash value. I could post the html form if need be.

      Thanks,
      -Katie
        PerlMonks messes up the first table, so it's in code tags. I also had HTML::BarGraph laying around, but I never use it, so no example ;)
        use HTML::BarChart; use CGI qw[ *table Tr td br Th ]; my $survey = 'I think pasta is good'; my %survey = ( 'Strongly agree' => '********', 'Agree' => '**', 'Disagree' => '*************', 'Strongly disagree' => '****' ); my $flippy = 0; my $Chart = HTML::BarChart->new( $survey, 100, 100 ); foreach my $key ( keys %survey ) { $Chart->bar( $key, length $survey{$key}, $flippy ? $flippy++ && "red" : $flippy-- && "black" ); } $Chart->draw; print start_table({-border=>1}),Th({-colspan=>2}, $survey ); foreach my $key ( keys %survey ) { print Tr( td({ -height => "30", -bgcolor => "red" },$key), td($survey{$key} ) ); } print end_table; __END__ <table bgcolor="#000000" width="100" height="100" border="0" cellp +adding="0" cellspacing="1"><tr> <td colspan="5"> <table border="0" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF +" width="100%"> <tr><td valign="top" align="center"><font size="2" face="Arial"> I think pasta is good </td></tr></table> </td></tr><tr><td colspan="5" align="center"><table bgcolor="#FFFFFF" +border="0" cellpadding="0" cellspacing="0" width="100%"><tr> <td valign="bottom" align="center" width="100"> <table height="30.7692307692308" width="25" bgcolor="0" cellpa +dding="1" cellspacing="1"> <tr><td></tr></td></table> </td> <td valign="bottom" align="center" width="100"> <table height="100" width="25" bgcolor="red" cellpadding="1" c +ellspacing="1"> <tr><td></tr></td></table> </td> <td valign="bottom" align="center" width="100"> <table height="15.3846153846154" width="25" bgcolor="0" cellpa +dding="1" cellspacing="1"> <tr><td></tr></td></table> </td> <td valign="bottom" align="center" width="100"> <table height="61.5384615384615" width="25" bgcolor="red" cell +padding="1" cellspacing="1"> <tr><td></tr></td></table> </td> </tr></table></td></tr><tr><td><table width="100%" border="0" cellpadd +ing="0" cellspacing="1" bgcolor="#FFFFFF"><tr> <td align="center"><font size="1" face="Arial">Strongly disagree</ +font></td></tr></table></td> <td><table width="100%" border="0" cellpadding="0" cellspacing="1" bgc +olor="#FFFFFF"><tr> <td align="center"><font size="1" face="Arial">Disagree</font></td +></tr></table></td> <td><table width="100%" border="0" cellpadding="0" cellspacing="1" bgc +olor="#FFFFFF"><tr> <td align="center"><font size="1" face="Arial">Agree</font></td></ +tr></table></td> <td><table width="100%" border="0" cellpadding="0" cellspacing="1" bgc +olor="#FFFFFF"><tr> <td align="center"><font size="1" face="Arial">Strongly agree</fon +t></td></tr></table></td> </tr></table>
        I think pasta is good
        Strongly disagree ****
        Disagree *************
        Agree **
        Strongly agree ********

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

        <table border="1"> <tr> <td width="40%" align="right">QUESTION_TEXT</td> <td width="60%"> <table width="20" cellspacing="0" cellpadding="5" border="0 +"> <tr bgcolor="red"><td align="center"><strong>20</strong +></td></tr> </table> </td> </tr> <tr> <td width="40%" align="right">QUESTION_TEXT</td> <td width="60%"> <table width="30" cellspacing="0" cellpadding="5" border="0 +"> <tr bgcolor="red"><td align="center"><strong>30</strong +></td></tr> </table> </td> </tr> <tr> <td width="40%" align="right">QUESTION_TEXT</td> <td width="60%"> <table width="40" cellspacing="0" cellpadding="5" border="0 +"> <tr bgcolor="red"><td align="center"><strong>40</strong +></td></tr> </table> </td> </tr> <tr> <td width="40%" align="right">QUESTION_TEXT</td> <td width="60%"> <table width="50" cellspacing="0" cellpadding="5" border="0 +"> <tr bgcolor="red"><td align="center"><strong>50</strong +></td></tr> </table> </td> </tr> </table>
        Try this HTML in your browser. Are you looking for something like that?

        --dda

        HOW TO CODE AN OPINION POLL WITH MYSQL AS BACKEND i am using Message Master as a platform for Cellphones. I code on Perl and the database is MYSQL. the arguments to be taken are. YES;No;Time;CellNumber;counter

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2025-03-20 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (60 votes). Check out past polls.