#!/perl/bin/perl -w use CGI; $q = new CGI; for $key ( $q->param() ) { $form{$key} = $q->param($key); } $logpath = "data/browser.dat"; $temppath = "C:/Documents and Settings/Owner/My Documents/My Website/c +gi-bin/data/temp.dat"; $browser = $form{'browser'}; open (LOG, "$logpath"); @data = <LOG>; close(LOG); $not_found = 1; if($browser) { open (LOG, ">$logpath"); foreach $line(@data) { chomp($line); ($fcount,$fbrowser)= split(/\|/,$line); if ($fbrowser eq $browser) { $not_found = 0; $fcount = $fcount+1; print LOG "$fcount|$fbrowser\n"; } else { print LOG "$line\n"; } } close(LOG); if($not_found) { open (LOG,">>$logpath"); print LOG "1|$browser"; close(LOG); } } # prints out log and form to browser print "Content-type: text/html\n\n"; open (LOG, "$logpath"); @data = <LOG>; close(LOG); foreach $line(@data) { chomp($line); print "$line<br>"; } close(LOG); print <<EOT; <FORM ACTION="basic.pl" METHOD="POST"> <SELECT NAME="browser"> <OPTION VALUE="microsoft">microsoft</OPTION> <OPTION VALUE="netscape">netscape</OPTION> <OPTION VALUE="aol">aol</OPTION> <OPTION VALUE="mozilla">mozilla</OPTION> <OPTION VALUE="opera">opera</OPTION> </SELECT> <INPUT TYPE="SUBMIT" VALUE="Submit"> </FORM> EOT # If you would like to suggest a better way to sort here is the rest o +f the code. sub getBrowsers { %browse=(); open(INF,"$logpath") || print "Cannot open logs.dat file\n"; @hits= <INF>; close(INF); open(TEMP,">>$temppath") || print "Cannot open temp.dat file\n"; foreach $i (@hits) { chomp($i); ($hits,$browser)= split(/\|/,$i); for ($t=1;$t<$hits;$t++) { print TEMP "$browser\n"; } } close(TEMP); open(TEMP,"$temppath") || print "Cannot open temp.dat file\n"; @temp= <TEMP>; close(TEMP); foreach $i (@temp) { $browse{$i}++; } print <<EOT; <TABLE width="99%" cellpadding="3" cellspacing="1" border="0" bgCo +lor="#0080ff"> <TR><TD bgColor="#ccccff" height="25" colspan="2"><SPAN class="tit +le">Top Browsers</SPAN></TD></TR> EOT $max = 1; foreach $key (sort BrowserSort (keys(%browse))) { ($max > $browse{$key}) || ($max = $browse{$key}); ($max >= 401) && ($divisor = $max/401); ($max < 401) && ($multiplier = 401/$max); ($divisor) && ($width = sprintf("%0d",$browse{$key}/$divisor)); ($multiplier) && ($width = sprintf("%0d",$browse{$key} * $multipli +er)); print "<tr><td bgColor=\#FFFFFF align=left><a href=$key>$key</a><b +r><img src=../images/pix.gif width=1 height=2><br> <img src=../images/blue.gif width=$width height=10></td><td bg +Color=\#FFFFFF align=right valign=bottom>$browse{$key}</td></tr>\n"; } print "</table><p>\n"; } sub BrowserSort { $browse{$b} <=> $browse{$a}; } &getBrowsers; unlink($temppath);

In reply to I have it working, but sorting is very greedy. by Anonymous Monk
in thread Need help with searching flatfile and updating it. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.