Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Error in code - read-only value?

by katgirl (Hermit)
on Sep 23, 2003 at 12:53 UTC ( [id://293476]=perlquestion: print w/replies, xml ) Need Help??

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

*sigh* The following code seems to work fine on my PC but I get an error message (one I haven't managed to come up with before) when I run it from my web site.
#!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use CGI; my $req = new CGI; my $sort = $req->param("sort"); if(!$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"); my @files = grep( /\.dat/, readdir(DIR) ); @files = sort(@files); closedir(DIR); $num = 0; foreach $file(@files){ open (TXTFILE, "members/$file") or die "Can't open $file"; @lines = <TXTFILE>; close(TXTFILE); tr/\r\n//d for @lines; $hit = $lines[0]; if($hit >0){ $info = sprintf("%.2f", ($lines[1]/$hit) ); $graphics = sprintf("%.2f", ($lines[2]/$hit) ); $friendly = sprintf("%.2f", ($lines[3]/$hit) ); $techno = sprintf("%.2f", ($lines[4]/$hit) ); $entertain = sprintf("%.2f", ($lines[5]/$hit) ); $rate = sprintf("%.2f", (($info + $graphics + $frien +dly + $techno + $entertain)/5) ); $id = $file; $id =~ s/\.dat//; $all_hashes[$num] = ({ ID => $id, info => $info, graphics => $graphics, friendly => $friendly, techno => $techno, entertain => $entertain, rate => $rate, hit => $hit, }); }#end if hit $num++; }#end for each file if($sort eq "id" ){ @all_hashes = sort { $a->{ID} <=> $b->{ID} } @all_ +hashes;} if($sort eq "info" ){ @all_hashes = sort { $a->{info} <=> $b->{info} } + @all_hashes;} if($sort eq "graphics" ){ @all_hashes = sort { $a->{graphics} <=> $b-> +{graphics} } @all_hashes;} if($sort eq "friendly" ){ @all_hashes = sort { $a->{friendly} <=> $b-> +{friendly} } @all_hashes;} if($sort eq "techno" ){ @all_hashes = sort { $a->{techno} <=> $b->{tec +hno} } @all_hashes;} if($sort eq "entertain" ){ @all_hashes = sort { $a->{entertain} <=> $b +->{entertain} } @all_hashes;} if($sort eq "average" ){ @all_hashes = sort { $a->{rate} <=> $b->{rate +} } @all_hashes;} if($sort eq "hits" ){ @all_hashes = sort { $a->{hit} <=> $b->{hit} } @ +all_hashes;} for(@all_hashes){ print "<tr><td>$_->{ID}</td>\n<td>$_->{info}</td>\n<td>$_->{graph +ics}</td>\n<td>$_->{friendly}</td>\n<td>$_->{techno}</td>\n<td>$_->{e +ntertain}</td>\n<td>$_->{rate}</td>\n<td>$_->{hit}</td>\n</tr>"; } print "</table>";
The error message is:
Click on a header to sort. ID Info Graphics Friendly Techno Entertain Average Hits Content-type: text/html Software error: Modification of a read-only value attempted at line 50. For help, please send mail to the webmaster (support@aplus.net), givin +g this error message and the time and date of the error.
Line 50 is if($sort eq "id" ){ @all_hashes = sort { $a->{ID} <=> $b->{ID} } @all_hashes;} What have I done (now)?

Replies are listed 'Best First'.
Re: Error in code - read-only value?
by asarih (Hermit) on Sep 23, 2003 at 13:36 UTC
    sort returns a sorted list, so sort {$a->{ID} <=> $b->{ID} } @all_hashes (without the assignment) should be sufficient.

    Not: Grossly uninformed post here. You'd want to say

    @sorted_all_hashes = sort {$a->{ID} <=> $b->{ID}} @all_hashes
      Thanks - Revised code (with the help of Mr. Muskrat also) -
      #!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use CGI; my $req = new CGI; my $sort = $req->param("sort"); if(!$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"); my @files = grep( /\.dat/, readdir(DIR) ); @files = sort(@files); closedir(DIR); $num = 0; foreach $file(@files){ open (TXTFILE, "members/$file") or die "Can't open $file"; @lines = <TXTFILE>; close(TXTFILE); tr/\r\n//d for @lines; $hit = $lines[0]; if($hit >0){ $info = sprintf("%.2f", ($lines[1]/$hit) ); $graphics = sprintf("%.2f", ($lines[2]/$hit) ); $friendly = sprintf("%.2f", ($lines[3]/$hit) ); $techno = sprintf("%.2f", ($lines[4]/$hit) ); $entertain = sprintf("%.2f", ($lines[5]/$hit) ); $rate = sprintf("%.2f", (($info + $graphics + $frien +dly + $techno + $entertain)/5) ); $id = $file; $id =~ s/\.dat//; $all_hashes[$num] = ({ ID => $id, info => $info, graphics => $graphics, friendly => $friendly, techno => $techno, entertain => $entertain, rate => $rate, hit => $hit, }); }#end if hit $num++; }#end for each file sortby($sort, \@all_hashes); for(@all_hashes){ print "<tr><td>$_->{ID}</td>\n<td>$_->{info}</td>\n<td>$_->{graph +ics}</td>\n<td>$_->{friendly}</td>\n<td>$_->{techno}</td>\n<td>$_->{e +ntertain}</td>\n<td>$_->{rate}</td>\n<td>$_->{hit}</td>\n</tr>"; } print "</table>"; sub sortby { my $key = shift; my $aref = shift; sort { $a->{$key} <=> $b->{$key} } @$aref; }
      But now it's sorting the keys in text order (1 - 100 - 101 - 2 - 21 etc) and when I click on the links to change the sorted by order, it still comes out sorted by ID.

        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 :-)

Re: Error in code - read-only value?
by katgirl (Hermit) on Sep 23, 2003 at 14:21 UTC
    Now I'm back to the original error! :(

    Here's some data for you:

    1.dat 1 10 9 8 9 6 2.dat 5 48 39 50 46 45 3.dat 4 40 39 30 36 35
    Update Error has now moved to Line 61 -
    return sort { $a->{$key} <=> $b->{$key} } @$aref;
    Gaaaaaaaaah! Update 2 Thanks to Mr. Muskrat and atcroft for help - Code is now the same as the above node, but with the following change:
    foreach my $c( &sortby($sort, \@all_hashes) ){ print "<tr><td>$c->{'id'}</td>\n<td>$c->{'info'}</td>\n<td>$c->{'gra +phics'}</td>\n<td>$c->{'friendly'}</td>\n<td>$c->{'techno'}</td>\n<td +>$c->{'entertain'}</td>\n<td>$c->{'average'}</td>\n<td>$c->{'hits'}</ +td>\n</tr>"; }
    Output is OK, but the first 20 lines of the table are blank! Weird!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-24 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found