Solved

running average script

Posted on 1998-11-18
3
234 Views
Last Modified: 2008-02-01
How can I build/get a script that gives a running average of 5 inputs? Example situation would be rating a given thing in 5 categories each category having a 1 to 5 scale. So a viewer would see the running average results of many people rating this thing in 5 categories. Can anyone help?
0
Comment
Question by:kidvid
3 Comments
 
LVL 2

Expert Comment

by:Quixote
Comment Utility
KidVid-

Your 500 points will gain a lot of attention, and maybe someone here on the JavaScript board knows enough PERL to help you out, but this is a server-side question.  JavaScript can't possibly know what other people are ranking those this as, except to download a file that has the numbers.  In order to create that file, you need to output it dynamicly from a CGI, or use a CGI to write out a static file.  Either way, you need access to the server.

Good luck.  Cheers.
-Quixote
0
 
LVL 10

Expert Comment

by:MasseyM
Comment Utility
What type of server?  If it is IIS, I can do some ASP stuff forr you (Especially if the info is stored in a database).
0
 
LVL 10

Accepted Solution

by:
kollegov earned 500 total points
Comment Utility
1. you cant do this at client side (javaScript)
Java script have no info about other users, and due to security restrictions there is no chance to know about other users votes.

2. Goin ahead and posting Perl CGI script for this
(note that you should have administrative rights to run Perl scripts at your server)

This solution consists of two Perl scripts
Script to output results (outrates.cgi)
Script to make vote (rate.cgi)
rate.dat local file for storing data
and HTML document.

--------------outrates.cgi file---------
#!/usr/bin/perl


#--------------------------------------------
# place here actual path to file rate.dat file
$cfile     = "c:/omni/htdocs/kaka/rate.dat";
#

$height    =100;    # table height
$width     =20;     # table coloumn width
$maxrate   =5;      # maximum rating
$ncounts   =5;      # number of coloumns

$tw = ($width+2)*$ncounts+2;

print <<EOF;
Content-type: text/html


<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT=5>
</head>
<body><table border=0 cellspacing=0 cellpadding=0>
<tr ><TD colspan=7><IMG SRC="/kaka/redpoint.gif" width=$tw height=1 border=0></TD></tr>
<tr valign=bottom>
<TD><IMG SRC="/kaka/redpoint.gif" width=1 height=$height border=0></TD>
EOF

open FILE,"$cfile" || die ("Can't open rates file!");
while(<FILE>)
  {($total,$num) = split(/ /);
   if($num>0)
    {use integer;
     $aver = $total*$height/$num/$maxrate;
     print <<EOF;
     <td valign=bottom height=$height><IMG
      SRC="/kaka/pixel.gif" width=1 height=1
      border=0><IMG
      SRC="/kaka/redpoint.gif" width=$width height=$aver border=0><IMG
      SRC="/kaka/pixel.gif" width=1 height=1
      border=0></td>
EOF
    }
   else
    {print <<EOF;
     <td valign=bottom height=$height><IMG
      SRC="/kaka/pixel.gif" width=1 height=1
      border=0><IMG
      SRC="/kaka/pixel.gif" width=$width height=1 border=0><IMG
      SRC="/kaka/pixel.gif" width=1 height=1
      border=0></td>
EOF
    }
 
  }
close (FILE);

print <<EOF;

<TD><IMG SRC=\"/kaka/redpoint.gif\" width=1 height=$height border=0></TD>
</tr>
<tr ><TD colspan=7><IMG SRC=\"/kaka/redpoint.gif\" width=$tw height=1 border=0></TD></tr>
</table></body></html>

EOF

------------end of file---------



Next one is a little bit wired, to have a good protection
between several simultanious votes
---------------------rate.cgi file----------
#!/usr/bin/perl

#-----------------------------------
#place here actual path to directory where
# rate.dat file will be stored.
$tmp        = "c:/omni/htdocs/kaka/";

$datafile   =$tmp."rate.dat";
$databack   =$tmp."rate.bak";
$myerrorfile=$tmp."error.txt";
$lockfile   =$tmp."ratelock.txt";


$QUERY_STRING= $ENV{'QUERY_STRING'};
for (split (/&/, $QUERY_STRING)) {
    ($name, $value) = &unescape(split (/=/));
    $get{$name} = $value;
}

print <<EOF;
Content-type: text/html

<html>
<body>
EOF

$item=$get{'item'};
$rate=$get{'rate'};
if($rate && $item)
  {if(($rate>0) && ($rate<=5))
    {if(setlock()==1)
       {rename($datafile,$databack);
        open (NEW,">$datafile") || return 0;
        open (OLD, "$databack") || return 0;
        chmod (0664,$datafile,$databack);
        $i=1;
        while (<OLD>)
          {($t,$n)=split(/ /);            
           if($i==$item)
             {$t=$t+$rate;
              $n++;
              print NEW "$t $n\n";
             }
           else {print NEW "$_";}
           $i++;
          }
        close(OLD);
        close(NEW);
        print "Your input counted, thanks";
        removelock();
       }
     else
       {print "Busy, try later";}
    }
  }

print <<EOF;  
</body></html>
EOF



#------------------------
sub restorebackup
{if(-f $datafile)
   {unlink($datafile) || die "Can't remove datafile $!";}
 open (NEW,">$datafile") || die "RestoreBackup: Can't create new data file $!";
 open (OLD, "$databack") || die "RestoreBackup: Can't open old   data file $!";
 chmod (0664,$datafile,$databack);
 while(<OLD>)
    {print NEW "$_";}
 close(OLD);
 close(NEW);
}

#---------------------
sub setlock
{#-------------------------
 # return 1 on success otherwise 0
 #---check lock file-------

 if (-f $lockfile)
  {if (-M $lockfile > (1/(24*4)))     #15 minutes
    {unlink ("$lockfile") || return 0;
     $restore=1;
    }
   else
    {for ($i = 60; $i > 0; --$i)
      {sleep(1);
       last unless -f $lockfile;
      }
    }
  }
 open (LOCKFILE,">$lockfile") || return 0;
 if (! -f $lockfile) {return 0;}
 close(LOCKFILE);

 chmod 0664,$lockfile;
 if(! -f $datafile)
    {open(FILE, ">$datafile") || return 0;
     chmod 0664,$datafile;
     close (FILE);
    }
 if(! -f $databack)
    {open(FILE, ">$databack") || return 0;
     chmod 0664,$databack;
     close (FILE);
    }

 if($restore) {restorebackup();}
 return 1;
}


sub removelock
{unlink ("$lockfile") || print STDERR "Can't remove lockfile";}

#---------------------
sub unescape
{for (@_) {s/\+/ /g;
           s/%([\da-f]{2})/pack("C",hex $1)/gei;
          }
 return @_;
}

------------end of file---------


Now you need to create initial rate.dat file
------file rate.dat------
0 0
0 0
0 0
0 0
0 0
------------end of file-----
This file should contain 5 lines with total summ and number of votes, place it accordingly to paths you use in scripts
(see comments in scripts)
Note also that this file SHOULDN'T have any empty lines.

Last thing HTMLs.
Note that outrates.cgi creates HTML on-fly with output
And Meta REFRESH tag, so file will be reloaded each 5 sec itself
You can use frameset to put this output in one of the frames
<FRAME SRC="/cgi-bin/outrates.cgi">

Last thing you need is a form to make vote somewhere in other HTML in other frame
here it is:

<html>
<form action="/cgi-bin/counters/rate.cgi">
 <SELECT NAME="item">
 <OPTION VALUE=1>Item a
 <OPTION VALUE=2>Item b
 <OPTION VALUE=3>Item c
 <OPTION VALUE=4>Item d
 <OPTION VALUE=5>Item e
 </SELECT>
 <SELECT NAME="rate">
 <OPTION VALUE=1>Bad
 <OPTION VALUE=2>not bad
 <OPTION VALUE=3>SoSo
 <OPTION VALUE=4>Good
 <OPTION VALUE=5>Exelent
 </SELECT>
 <INPUT TYPE="submit" value="rate">

</form>
</html>

Or you can just make a links
<A HREF="/cgi-bin/rate.cgi?item=2&rate=3">Rate Item2 as 3</a>
Just put there actual values in range 1..5;

Hope this helps...

------
Well, I know all this probably looks a little bit wired, but
THERE IS NO OTHER WAY to do this.
Sure CGI can be written in other language, but you ANYWAY need CGI.
---------
Suggested solution tested under Netscape Fasttrack 2.0 server
And should work in ANY browser
(at least any one which handle tables and frames as HTML elements).
----------
Hope this Helps

Virtual_Max




0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now