Gossamer Forum
Home : Products : DBMan : Customization :

Sorting

Quote Reply
Sorting
Hi if i have the following sub

sub get_average {
# --------------------------------------------------------
# Get the "average" value of a field.
#

my $which_id = shift;
my $id_pos = 0;
my $rating_pos = 22;
my $which_db = $db_script_path . "/rating.db";
my $avg_count = 0; # Initialise the counter
my $avg_total = 0; # Initialise the total

open (DB, "<$which_db") or &cgierr("Unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;

foreach $line (@lines) {
chomp ($line);
@tmp_record = split (/\Q$db_delim\E/o, $line);
if ($tmp_record[$id_pos] eq $which_id) {
$avg_count++; # Count up each record
$avg_total = $avg_total + $tmp_record[$rating_pos]; # Add rating to total
}
}
if ($avg_count > 0) {

$avg_total = int $avg_total / $avg_count; # Average the total by number of records
} else { $avg_total = 'Player not yet rated'; }

print $avg_total;

}

How could I sort on this sub if my short display looks like this.


<font size="-4"><A HREF="$db_script_link_url&ww=on&sb=$avg_total&$db_key=$rec{$db_key}&view_records=1">$rec{'Lastname'}</A></font></TD>
<td valign=top><font size="-4">$rec{'Firstname'}</font></td>
<td valign=top><font size="-4">$rec{'Team'}</font></td>
<td valign=top><font size="-4">$rec{'Month'}/$rec{'Day'}/$rec{'Year'}</font></td>
<td valign=top><font size="-4">|;&get_average("$rec2{'Playerid'}");print qq|</font></td>


I want to sort on the get average subroutine


Quote Reply
Re: Sorting In reply to
Does anyone have any idea about this

Quote Reply
Re: Sorting In reply to
Short of writing the average back out the DB, I'd think that attempting to pass the values to the existing sort subroutines might get a little hairy... can be done, yes, but an easier solution is just to right the values out to the DB whenever a record gets added or modified.

Quote Reply
Re: Sorting In reply to
So writing back to the database would be good but wouldn't it have to be wrote back to the one side of the database.If it was wrote to the many side how or to the one how, hmm I don't think that would work. The value comes form counting the playerid from the many side and then dividing by the number of occurences of them then gives me the average.

If it could be done just on the short view that would be the best for us, but can't figure out how? Basically sort on the routine values would be best

Quote Reply
Re: Sorting In reply to
ANyone!!