Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Re: Re: Error in code - read-only value?

by Mr. Muskrat (Canon)
on Sep 23, 2003 at 14:17 UTC ( [id://293502]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Error in code - read-only value?
in thread Error in code - read-only value?

I've made a few changes and I'm not sure if it works since I don't have any data to test it on...

#!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use CGI; use strict; my $req = new CGI; my $sort = $req->param("sort"); if (!defined $sort){ $sort = "id"; } print "Content-type: text/html\n\n"; print "Click on a header to sort.<br>"; print "<table><tr><td><a href=\"?sort=id\">ID</a></td>\n<td><a href=\" +?sort=info\">Info</a></td>\n<td><a href=\"?sort=graphics\">Graphics</ +a></td>\n<td><a href=\"?sort=friendly\">Friendly</a></td>\n<td><a hre +f=\"?sort=techno\">Techno</a></td>\n<td><a href=\"?sort=entertain\">E +ntertain</a></td>\n<td><a href=\"?sort=average\">Average</a></td>\n<t +d><a href=\"?sort=hits\">Hits</a></td>\n</tr>"; opendir(DIR, "members") or die "Can't open the members directory, $!"; my @files = grep( /\.dat/, readdir(DIR) ); @files = sort(@files); closedir(DIR); my @all_hashes; my $num = 0; foreach my $file (@files){ open (TXTFILE, "members/$file") or die "Can't open $file"; my @lines = <TXTFILE>; close(TXTFILE); tr/\r\n//d for @lines; my $hits = $lines[0]; if ($hits > 0) { my $info = sprintf("%.2f", ($lines[1]/$hits) ); my $graphics = sprintf("%.2f", ($lines[2]/$hits) ); my $friendly = sprintf("%.2f", ($lines[3]/$hits) ); my $techno = sprintf("%.2f", ($lines[4]/$hits) ); my $entertain = sprintf("%.2f", ($lines[5]/$hits) ); my $average = sprintf("%.2f", (($info + $graphics + $friendly + $t +echno + $entertain)/5) ); my $id = $file; $id =~ s/\.dat//; $all_hashes[$num] = ({ id => $id, info => $info, graphics => $graphics, friendly => $friendly, techno => $techno, entertain => $entertain, average => $average, hits => $hits, }); } # end if hits $num++; } # end for each file @all_hashes = sortby($sort, \@all_hashes); for (@all_hashes){ print "<tr><td>$_->{id}</td>\n<td>$_->{info}</td>\n<td>$_->{graphics +}</td>\n<td>$_->{friendly}</td>\n<td>$_->{techno}</td>\n<td>$_->{ente +rtain}</td>\n<td>$_->{average}</td>\n<td>$_->{hits}</td>\n</tr>"; } print "</table>"; sub sortby { my $key = shift; my $aref = shift; my @array = @$aref; return sort { $a->{$key} <=> $b->{$key} } @array; }

Update: You didn't have an 'average' or a 'hits' key in the hash so I did a search and replace and this what I ended up with.

Update 2: Now strict compliant.

Update 3: No more read-only value errors :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://293502]
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: (3)
As of 2024-04-24 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found