I am a Perl newbie who (until I started to try to sort my flatfile database) was able to modify what I found in books and in tutorials.
I have pieced together a Perl program that will allow me to add, edit, and delete records in a pipe-delimited database. However, I cannot formulate a correct sort routine.
Here is the relevant portion of my code:
open (DATABASE,">$database");
@DB=<DATABASE>;
foreach $rec (@DATAB){
chomp($rec);
($name,$company,$score)=split(/\|/,$rec);
if ($name eq $input{'name'} && $company eq $input{'company'} && $score eq $input{'score'}){
print DATABASE "$input{'nname'}|$input{'ncompany'}|$input{'nscore'}\n";
}else{
print DATABASE "$name|$company|$score\n";
}
}
close (DATABASE);
@DATABASE = sort {
$score{$b} <=> $score{$a} or $company{$a} cmp $company{$b} or $name{$a} cmp $name{$b}
} @DATAB;
foreach $rec (@DATABASE){
chomp($rec);
($name,$company,$score)=split(/\|/,$rec);
print DATABASE "$name|$company|$score\n";
}
Can anyone please tell me what I am screwing up? (This code will run without error, but it does not sort.)
I have pieced together a Perl program that will allow me to add, edit, and delete records in a pipe-delimited database. However, I cannot formulate a correct sort routine.
Here is the relevant portion of my code:
Code:
open (DATABASE,">$database");
@DB=<DATABASE>;
foreach $rec (@DATAB){
chomp($rec);
($name,$company,$score)=split(/\|/,$rec);
if ($name eq $input{'name'} && $company eq $input{'company'} && $score eq $input{'score'}){
print DATABASE "$input{'nname'}|$input{'ncompany'}|$input{'nscore'}\n";
}else{
print DATABASE "$name|$company|$score\n";
}
}
close (DATABASE);
@DATABASE = sort {
$score{$b} <=> $score{$a} or $company{$a} cmp $company{$b} or $name{$a} cmp $name{$b}
} @DATAB;
foreach $rec (@DATABASE){
chomp($rec);
($name,$company,$score)=split(/\|/,$rec);
print DATABASE "$name|$company|$score\n";
}
Can anyone please tell me what I am screwing up? (This code will run without error, but it does not sort.)