Avatar billede flannagan Nybegynder
16. oktober 2002 - 23:31 Der er 6 kommentarer og
1 løsning

Slet decimaler i CGI poll

Hej eksperter.

Jeg roder lidt med et poll-script jeg fandt på nettet.
Jeg kan bare ikke få %-resultaterne afrundet.
F. eks ser de 3 %-resultater sån' her ud:
77,777777777777778
16,666666666666667
5,555555555555556

.. -og det ser jo ikke så smart ud!

Her er min cgi-fil:

#!/usr/bin/perl

if ($ENV{'REQUEST_METHOD'} eq 'POST') {

      # Get the input

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

      # Split the name-value pairs

@pairs = split(/&/, $buffer);

      # Load the FORM variables

foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

      $FORM{$name} = $value;
}
}

print "Content-type: text/html\n\n";
if(-e "poll.dat")
{
      open(POLL, "poll.dat");
}

while(<POLL>)
{
      push(@poll, $_);
}
close(POLL);

@lines = ("1");
foreach $each_line (@lines)
{
      $this_line = "line" . $each_line;
      $$this_line = $poll[$each_line-1];
      chop($$this_line);
}

if($line1 eq " ")
{
      $line1 = "0\t0\t0";
}

@line1_answers = split(/\t/, $line1);

if($FORM{'QUESTION'} eq "brian")
{
      $line1_answers[0]++;
}

elsif($FORM{'QUESTION'} eq "beckham")
{
    $line1_answers[1]++;
}

elsif($FORM{'QUESTION'} eq "pitt")
{
    $line1_answers[2]++;
}

open(POLL, ">poll.dat");
print POLL "$line1_answers[0]\t$line1_answers[1]\t$line1_answers[2]\n";
close(POLL);

print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>POLL RESULTS</TITLE>\n";
print "</HEAD>\n";
print "<BODY BGCOLOR=FFFFFF>\n";
print "<BODY BACKGROUND=../images/video/video_bgright.jpg>\n";
print "<CENTER><H2>Poll Results</H2></CENTER>\n";
print "<font color=FFFFFF size=1 face=Arial>Hvem er mest sexet?<BR>\n";

print "<TABLE BORDER=0 cellpadding=3 cellspacing=1>";

print "<TR>";
print "<TD><font color=FFFFFF size=1 face=Arial><b>Answer</b></TD><TD><font color=FFFFFF size=1 face=Arial><b>Number</b></TD><TD><font color=FFFFFF size=1 face=Arial><b>Percentage</b></TD><TD><font color=FFFFFF size=1 face=Arial><b>Visual</b></TD>";
print "</TR>";

print "<TR>";
print "<TD><font color=FFFFFF size=1 face=Arial>";
print "Brian: ";
print "</TD>";

print "<TD><font color=FFFFFF size=1 face=Arial>";
print $line1_answers[0];
print "</TD>";

print "<TD><font color=FFFFFF size=1 face=Arial>";
print $line1_answers[0] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100;
print "%";
print "</TD>";

print "<TD>";
print "<IMG SRC=../poll/images//pollred.gif height=7 width=";
print $line1_answers[0] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100;
print " ALT=Yes answers>";
print "</TD>";
print "</TR>";

print "<TR>";
print "<TD><font color=FFFFFF size=1 face=Arial>";
print "Beckham: ";
print "</TD>";

print "<TD><font color=FFFFFF size=1 face=Arial>";
print $line1_answers[1];
print "</TD>";

print "<TD><font color=FFFFFF size=1 face=Arial>";
print $line1_answers[1] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100;
print "%";
print "</TD>";

print "<TD>";
print "<IMG SRC=../poll/images/pollblue.gif height=7 width=";
print $line1_answers[1] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100;
print " ALT=No answers>";
print "</TD>";
print "</TR>";

print "<TR>";
print "<TD><font color=FFFFFF size=1 face=Arial>";
print "Pitt: ";
print "</TD>";

print "<TD><font color=FFFFFF size=1 face=Arial>";
print $line1_answers[2];
print "</TD>";

print "<TD><font color=FFFFFF size=1 face=Arial>";
print $line1_answers[2] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100;
print "%";
print "</TD>";
print "<TD>";
print "<IMG SRC=../poll/images//pollgreen.gif height=7 width=";
print $line1_answers[2] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100;
print " ALT=Maybe answers>";
print "</TD>";
print "</TR>";

print "<TR>";
print "<TD colspan=4><font color=FFFFFF size=1 face=Arial>";
print "<b>Total Number of Answers:</b> ";
print $line1_answers[0] + $line1_answers[1] + $line1_answers[2];
print "</TD>";
print "</TR>";

print "</TABLE>";


print "</BODY>\n";
print "</HTML>\n";
Avatar billede thomasledet Nybegynder
16. oktober 2002 - 23:46 #1
smid dine resultater ind i int($resultat), så bliver alt efter punktummet skippet
Avatar billede thomasledet Nybegynder
16. oktober 2002 - 23:47 #2
hvis du vil have dine værdier afrundet pænt, kan du gøre følgende:

$afrundetresultat = int(($resultat+.5));
Avatar billede flannagan Nybegynder
16. oktober 2002 - 23:59 #3
Spørger måske dumt, men hvor skal jeg smide linien ind?
Sorry, men jeg er ikke programmør:-)
Avatar billede flannagan Nybegynder
17. oktober 2002 - 00:09 #4
Det virker alligevel.
Jeg uploadede i binary mode...

Tak for hjælpen!
Avatar billede thomasledet Nybegynder
17. oktober 2002 - 00:26 #5
#!/usr/bin/perl

if ($ENV{'REQUEST_METHOD'} eq 'POST') {

      # Get the input

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

      # Split the name-value pairs

@pairs = split(/&/, $buffer);

      # Load the FORM variables

foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

      $FORM{$name} = $value;
}
}

print "Content-type: text/html\n\n";
if(-e "poll.dat")
{
      open(POLL, "poll.dat");
}

while(<POLL>)
{
      push(@poll, $_);
}
close(POLL);

@lines = ("1");
foreach $each_line (@lines)
{
      $this_line = "line" . $each_line;
      $$this_line = $poll[$each_line-1];
      chop($$this_line);
}

if($line1 eq " ")
{
      $line1 = "0\t0\t0";
}

@line1_answers = split(/\t/, $line1);

if($FORM{'QUESTION'} eq "brian")
{
      $line1_answers[0]++;
}

elsif($FORM{'QUESTION'} eq "beckham")
{
    $line1_answers[1]++;
}

elsif($FORM{'QUESTION'} eq "pitt")
{
    $line1_answers[2]++;
}

open(POLL, ">poll.dat");
print POLL "$line1_answers[0]\t$line1_answers[1]\t$line1_answers[2]\n";
close(POLL);

print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>POLL RESULTS</TITLE>\n";
print "</HEAD>\n";
print "<BODY BGCOLOR=FFFFFF>\n";
print "<BODY BACKGROUND=../images/video/video_bgright.jpg>\n";
print "<CENTER><H2>Poll Results</H2></CENTER>\n";
print "<font color=FFFFFF size=1 face=Arial>Hvem er mest sexet?<BR>\n";

print "<TABLE BORDER=0 cellpadding=3 cellspacing=1>";

print "<TR>";
print "<TD><font color=FFFFFF size=1 face=Arial><b>Answer</b></TD><TD><font color=FFFFFF size=1 face=Arial><b>Number</b></TD><TD><font color=FFFFFF size=1 face=Arial><b>Percentage</b></TD><TD><font color=FFFFFF size=1 face=Arial><b>Visual</b></TD>";
print "</TR>";

print "<TR>";
print "<TD><font color=FFFFFF size=1 face=Arial>";
print "Brian: ";
print "</TD>";

print "<TD><font color=FFFFFF size=1 face=Arial>";
print $line1_answers[0];
print "</TD>";

print "<TD><font color=FFFFFF size=1 face=Arial>";
print int(($line1_answers[0] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100)+0.5);
print "%";
print "</TD>";

print "<TD>";
print "<IMG SRC=../poll/images//pollred.gif height=7 width=";
print $line1_answers[0] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100;
print " ALT=Yes answers>";
print "</TD>";
print "</TR>";

print "<TR>";
print "<TD><font color=FFFFFF size=1 face=Arial>";
print "Beckham: ";
print "</TD>";

print "<TD><font color=FFFFFF size=1 face=Arial>";
print $line1_answers[1];
print "</TD>";

print "<TD><font color=FFFFFF size=1 face=Arial>";
print int(($line1_answers[1] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100)+0.5);
print "%";
print "</TD>";

print "<TD>";
print "<IMG SRC=../poll/images/pollblue.gif height=7 width=";
print int(($line1_answers[1] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100)+0.5);
print " ALT=No answers>";
print "</TD>";
print "</TR>";

print "<TR>";
print "<TD><font color=FFFFFF size=1 face=Arial>";
print "Pitt: ";
print "</TD>";

print "<TD><font color=FFFFFF size=1 face=Arial>";
print $line1_answers[2];
print "</TD>";

print "<TD><font color=FFFFFF size=1 face=Arial>";
print int(($line1_answers[2] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100)+0.5);
print "%";
print "</TD>";
print "<TD>";
print "<IMG SRC=../poll/images//pollgreen.gif height=7 width=";
print int(($line1_answers[2] / ($line1_answers[0] + $line1_answers[1] + $line1_answers[2])*100)+0.5);
print " ALT=Maybe answers>";
print "</TD>";
print "</TR>";

print "<TR>";
print "<TD colspan=4><font color=FFFFFF size=1 face=Arial>";
print "<b>Total Number of Answers:</b> ";
print $line1_answers[0] + $line1_answers[1] + $line1_answers[2];
print "</TD>";
print "</TR>";

print "</TABLE>";


print "</BODY>\n";
print "</HTML>\n";
Avatar billede thomasledet Nybegynder
17. oktober 2002 - 00:26 #6
sådan ca... det er ikke testet... hvis der er syntax-fejl skulle det være en nem sag lige at rette dem...
Avatar billede thomasledet Nybegynder
17. oktober 2002 - 00:27 #7
kanonnes...
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester