Gossamer Forum
Home : General : Perl Programming :

Sort problem

Quote Reply
Sort problem
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:

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.)
Quote Reply
Re: [sws_mark] Sort problem In reply to
You are storing the file in @DB but then looping @DATAB?...where did @DATAB come from?
Quote Reply
Re: [Paul] Sort problem In reply to
you are also sorting a hash that isn't defined anywhere in the code you provided.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [Paul] Sort problem In reply to
I suppose that I should supply more of the relevant code:

Code:


$database='pinball.dat';
$databaseview='scores.cgi';

&parse_form;

open (FILE,"<$database");
@DATAB=<FILE>;
close (FILE);

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";
}


The different arrays are used in differing parts of the entire program. There are no hashes.

Thanks to all for the input. This is great.
Quote Reply
Re: [sws_mark] Sort problem In reply to
>>
open (DATABASE,">$database");
@DB=<DATABASE>;
<<

You are opening $database for writing with that command and it will get emptied so @DB will always be empty.

The sort error is partly related to what "Fuzzy Thoughts" said, but also because you are trying to sort on entire un-split lines which sort() won't understand.
Quote Reply
Re: [Paul] Sort problem In reply to
Is my problem possibly also that I am trying to print to a file that is not open?
Quote Reply
Re: [sws_mark] Sort problem In reply to
If you don't mind changing your data structure to an array of hashrefs, then this should help you a little:

Code:
use strict;
my @data = ();

while (my $temp = <DATA>) {
chomp $temp;
my $rec = {};
($rec->{name}, $rec->{company}, $rec->{score}) = split /\|/, $temp;
push @data, $rec;
};

@data = sort {$b->{score} <=> $a->{score} or $a->{name} cmp $b->{name} or $a->{company} cmp $b->{company}} @data;

foreach my $rec (@data) {
print qq|$rec->{name} : $rec->{company} : $rec->{score}\n|;
}

__DATA__
foo|bar|7
foo|bar|5
foo|bark|6
blah|bing|3
this|hack|8
yack|foo|1
arg|joo|10

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy thoughts] Sort problem In reply to
Thank you very much.
Quote Reply
Re: [fuzzy thoughts] Sort problem In reply to
Quote:
($rec->{name}, $rec->{company}, $rec->{score}) = split /\|/, $temp;

Yummy hash slice...

Code:
@rec{qw/name company store/} = split /\|/, $temp;
Quote Reply
Re: [Paul] Sort problem In reply to
I was trying to do that at one point but couldn't remember how. I haven't sat down and written any Perl code in months.

Philip
------------------
Limecat is not pleased.