Gossamer Forum
Home : General : Perl Programming :

sorting problem

Quote Reply
sorting problem
i'm modifying some code that i found here awhile back. the main change is to pull this userids from the logfile instead of from the password file. but i see that my list is not being sorted. what's wrong please?
Code:
# modified to pull users from logfile instead of password file
my (@passdata);
open (PASS, "< $log_file") or &cgierr("error in view_log. unable to open log file: $log_file.\nReason: $!");
if ($db_use_flock) { flock(PASS, 1); }
while (<PASS>) {
next if ($_ =~ /^#/);
($sum_username) = split(/~/, $_);
unless ($i{$sum_username}++) {
push(@passdata, $sum_username);
}
}
close (PASS);

open (LOG, "< $log_file") or &cgierr("error in view_log. unable to open log file: $log_file.\nReason: $!");
if ($db_use_flock) { flock(LOG, 1); }
my @logdata = <LOG>;
close (LOG);

# changed for new log file format 1/7/2010
($who,$what,$clock,$when,$where) = split (/~/, $logdata[0]);

$date_start = $when;

$last_date = &get_date();


print qq|<table width="90%" cellpadding="2" cellspacing="1" border="0">
<tr><td colspan="6">Summary between $date_start - $last_date</td></tr>
<tr><th>User</th><th>Logged On</th><th>Records Added</th><th>Records Edited</th><th>Records Deleted</th><th>Mass

Mail</th></tr>|;
my $count = 0;
my ($sum_add, $sum_mod, $sum_del, $sum_log, @summary);
foreach $sum_userid (sort(@passdata)) {
$sum_add = 0;
$sum_mod = 0;
$sum_del = 0;
$sum_log = 0;
$sum_mail = 0;
for ($i = 0; $i <= $#logdata; $i++) {
($sum_id, $sum_action) = split(/~/, $logdata[$i]);
if ($sum_userid eq $sum_id) {
$sum_add++ if ($sum_action =~ /^added record/);
$sum_mod++ if ($sum_action =~ /^modified record/);
$sum_del++ if ($sum_action =~ /^deleted record/);
$sum_log++ if ($sum_action =~ /^logged/);
$sum_mail++ if ($sum_action =~ /mass mail/);
}
}
$summary[$count] = { userid => "$sum_userid",
added => "$sum_add",
modified => "$sum_mod",
deleted => "$sum_del",
logged => "$sum_log",
mail => "$sum_mail" };

$count++;
}
my @summary_sort = map { $_->[0] }
sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] }
map { [ $_, $_->{'logged'}, $_->{'added'} ] } (sort(@summary));

for ($i = $#summary_sort; $i >= 0; $i--) {

print qq|<tr>|;
print '<td>' . $summary_sort[$i]->{'userid'} . '&nbsp;&nbsp;</td>';
print '<td align=right>' . $summary_sort[$i]->{'logged'} . '&nbsp;&nbsp;</td>';
print '<td align=right>' . $summary_sort[$i]->{'added'} . '&nbsp;&nbsp;</td>';
print '<td align=right>' . $summary_sort[$i]->{'modified'} . '&nbsp;&nbsp;</td>';
print '<td align=right>' . $summary_sort[$i]->{'deleted'} . '&nbsp;&nbsp;</td>';
print '<td align=right>' . $summary_sort[$i]->{'mail'} . '&nbsp;&nbsp;</td>';
print qq|</tr>\n|;
}
print qq|</table>\n|;
i think the only things i changed were the name of the file to pull userlist (logfile instead of passfile) and the format of the file is delimited by tilde instead of spaces.

EDIT: i see now it's sorted by the 'logged' column in descending order. i want it sorted alphabetically in ascending order by the userid column.
thanks
Quote Reply
Re: [delicia] sorting problem In reply to
Stab in the dark - but what happens if you change:

Code:
map { [ $_, $_->{'logged'}, $_->{'added'} ] } (sort(@summary));

to:

Code:
map { [ $_, $_->{'userid'}, $_->{'added'} ] } (sort(@summary));

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] sorting problem In reply to
that did it! i also switched a and b in both places in the following line so it is ascending instead of descending:
Code:
sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] }

thanks again!
Quote Reply
Re: [delicia] sorting problem In reply to
Nice Angelic

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates