myelinviolin has asked for the wisdom of the Perl Monks concerning the following question:
Hey guys, I'm stuck with a sorting question. I keep getting the answer "Modification of a read-only value" at the sort. extract is a subroutine that extracts information from a file, and that is where the variables are defined ($name, etc.). I have successfully sorted things in the past, and have read (well, tried) a lot of things about this kind of error, but I am just not understanding the concept. The array prints out fine for me before and after the foreach loop, and I can't see any gaps in the information that would screw up a sort function. I am not defining anything in the extract subroutine as an array; everything is just a single variable. I've tried using strict and warnings and they didn't help with this problem. (should have used them from the beginning, but I didn't know how they worked and have gotten along fine without them so far). I was trying to use pop to put elements into the array, but the defining doesn't seem to be my issue here. What should I try looking at next in order to resolve this issue?
if($type eq 2){ @Files=<C:/perlscripts/Characters/*>; $v=1; foreach $file (@Files){ $A=join "",(split /\//,$file)[3]; #print "A:$A\n"; $char[$x]=join "", (split /\./,$A)[0]; $File_loc="C:\\perlscripts\\Characters\\"; $name=$char[$x]; $txt=".txt"; $FILE=$File_loc.$name.$txt; #print "$FILE\n"; open (CHAR_STATS, "$FILE")||die "Couldn't open the file, $!"; extract(CHAR_STATS); $S[$v]=[$name,$INIT,$DEX+$INIT_MISC,roll("1d20")]; print ".$v.\t.$S[$v][0].\t.$S[$v][1].\t.$S[$v][2].\t.$S[$v][3].\n"; $v++; } #pop(@S); print ".........\n"; $v--; for($y=1; $y<=$v; $y++){ print ".$v.\t.$S[$y][0].\t.$S[$y][1].\t.$S[$y][2].\t.$S[$y][3].\n"; } $a=(); $b=(); @G=sort{$a->[1] <=> $b->[1]||$a->[2] <=> $b->[2]||$a->[3] <=> $b->[3]} + @S; for (my $i=0; $i<=$#G; $i++) { print "$G[$i][0]\t$G[$i][1]\t$G[$i][2]\ +t$G[$i][3]\n"; } }
|
---|