Perl coding question - easy I think

Status
Not open for further replies.
I have a subroutine that opens a text file, which contains 50 or so lines, sorts them, and then leaves only the top 10. The code is below. Here is how my text file is setup (that gets sorted):

Name|Email|score

What the code does is it sorts the file by score, decending, and then only saves the top 10 records, by score.

PROBLEM: If multiple people have the same score, it won't include them all. It is only thinking the scores are unique. How can I modify the code so that it will sort but include people with ununique scores?

Thanks for any assistance!

code:<HR>
Code:
print "Content-type: text/html\n\n";
 	<P>use strict;
open(IN,"</usr/www/users/me/file.txt") or die $!;
chomp(my @lines = <IN> );
close IN;
my %cds;
foreach(@lines){
	my @a = split /\|/;
	$cds{$a[2]} = [ @a[0,1] ];
}
open(OUT,">/usr/www/users/me/file.txt") or die $!;
my $c = 0;
foreach(sort {$b <=> $a} keys %cds){
	print OUT join('|',@{$cds{$_}},$_) . "\n";
	$c++;
	last if $c == 10;
}
close OUT;
 
The problem is that your hashtable is being populated with non-unique keys (the score); what you want to do is use the email (probably your unique identifier, right?) as the key.

EDIT: A totally off the top of my head implementation (so no guarantee that it'll work, although it does compile):
code:<HR>
Code:
use strict;
print "Content-type: text/html\n\n";
open(IN,"</usr/www/users/me/file.txt") or die $!;<P>my (@cds, $c);<P>while (<IN>) {
  my @a;
  chomp; next if /^$/;
  @a = split /\|/, $_;
  push @cds, \@a;
}<P>@cds = @{ +sort { $a->[2] <=> $b->[2] } @cds }[0 .. 9];<P>open(OUT,">/usr/www/users/me/file.txt") or die $!;
for my $a (@cds) {
  print OUT join("|", @$a), "\n";
}<P>close OUT;
I'm using an array rather than a hashtable, for the usual no good reason. But the important bit is the sort command; it expects to get two array references, and sorts on the contents of the third field. I also just go ahead and grab the first 10 records with the array subscript there, too, although you have to disambiguate the sort keyword with a plus in there. Boy, perl sure is easy to read, eh?

As an exercise in total obscurity, the printing can be replaced as such:
code:<HR>
Code:
print OUT join("\n", map { join("|", @$_) } @{ +sort { $a->[2] <=> $b->[2] } @cds }[0 .. 9]);
Beautiful, ain't it?

Peace,
(jfb)

[This message was edited by nosferatu-man on November 26, 2001 at 14:10.]
 

Pont

Ars Legatus Legionis
26,160
Subscriptor
Keep an array of hashrefs and write your own sort routine.

something like:
code:<HR>
Code:
my @scores;
for (my $i = 0; my $line = <IN>; $i++) {
   chomp $line;
   my ($name, $email, $score) = split /|/, $line;
   $scores[$i]->{'name'} = $name;
   $scores[$i]->{'email'} = $email;
   $scores[$i]->{'score'} = $score;
}<P>@sorted_scores = sort { $a->{'score'} <=> $b->{'score'} } @scores;<P># @sorted_scores[0 - 9] holds the top 10 scores.

Untested and everything.
 
Originally posted by nathan:
Very simplistic solution:

Keep a variable that will always hold the score of the last record you printed out. If the score of the current record is the same as the score of the last record, don't increment your counter variable.

Make sense?

Umm.. Yeah, ignore that. I'll read the question more carefully next time, I promise. :oops:
 

THBMan

Ars Legatus Legionis
18,256
Subscriptor
My solution is to make that hash's data another layer's worth of array.

Like so:
code:<HR>
Code:
#!/usr/local/bin/perl
 
use strict;
 
print "Content-type: text/html\n\n";
 
open(IN,"/usr/www/users/me/file.txt") or die $!;
chomp(my @lines = <IN> );
close IN;
 
my %cds;
foreach(@lines){
  my @a = split /\|/;
  push @{$cds{$a[2]}}, [ @a[0,1] ];
}
 
open(OUT,">/usr/www/users/me/file.txt") or die $!;
my $c = 0;
foreach(sort {$b <=> $a} keys %cds){
  my $score = $_;
  foreach (@{$cds{$_}})
  {
    print OUT join('|',@$_,$score) . "\n";
    $c++;
    last if $c == 10;
  }
}
close OUT;

I made a test file with two lines having the same score, and both ended up being printed in the file properly.

<div align="right">The Happy Blues Man  </div>
 
Status
Not open for further replies.