UBB.Dev
Posted By: HunterGSXR Question about sorting and lists.. - 03/26/2002 9:01 AM
here's a chunk of code from public_pntf_summary.pl which is part of the Who's Online script..

i was interested in making it so the list of usernames shows up sorted by name instead of the random order it seems to be showing up in..

i'm a bit unfamiliar with Perl and was wondering how i'd take this list of User Numbers and convert it into a sorted list of User Names.

here's the block of code..

my @userstring;
my $counter = 0;
my @xa = @{ $r->{'sortorder'} };
my $diff = 0;

if($vars_pntf{CountOnly} eq "limit") {
if(scalar(@xa) > $vars_pntf{NameLimit}) {
$diff = scalar(@xa) - $vars_pntf{NameLimit};
@xa = @xa[0 .. ($vars_pntf{NameLimit} - 1)];
}
}

$r->{"overflow"} += $diff;

####### Here's where it takes the user number and adds it to the list to show in the Who's Online
SORTORDER: foreach my $number (@xa) {
my $pn = &GetPubName($number);
my $user_status = (&OpenProfile($number))[8];
next SORTORDER unless $pn;
push(@userstring, qq($wol_se_s$pn$wol_se_e));
}
Posted By: Dave_L Re: Question about sorting and lists.. - 03/26/2002 3:31 PM
Replace

Code
[/code]with

[code]
Posted By: HunterGSXR Re: Question about sorting and lists.. - 03/27/2002 9:26 AM
great. thanks. smile
Posted By: Dave_L Re: Question about sorting and lists.. - 03/27/2002 7:15 PM
It occurred to me that this could be simplfied a bit by using a hash, instead of an array of arrays, assuming that public names are unique. I'm not sure which method is more efficient, though.

[code][/code]
© UBB.Developers