Perl sorting question

Status
Not open for further replies.
Hi,
Im having a little Perl programming problem, I'm not too advanced, but this is what I want to do. I have a file, with about 30 or so lines like this, pipe delimited:

Name|Email|ID#

I need a subroutine that would open the file and sort all 30 or so lines by ID#, descending order, and THEN only keep the first ten lines, and delete the rest, i.e. so I would have the 10 highest ID numbers.

Any assistance would be surely appreciated!
 

Zen Shepherd

Ars Tribunus Angusticlavius
6,877
Subscriptor
this ain't terribly pretty but it should do ya:

code:<HR>
Code:
use strict;<P>open(IN,"<data.txt") or die $!;
chomp(my @lines = <IN>);
close IN;<P>my %cds;
foreach(@lines){
	my @a = split /\|/;
	$cds{$a[2]} = [ @a[0,1] ];
}<P>open(OUT,">output.txt") or die $!;
my $c = 0;
foreach(sort {$b <=> $a} keys %cds){
	print OUT join('|',@{$cds{$_}},$_) . "\n";
	$c++;
	last if $c == 10;
}
close OUT;

putting that into a sub should be trivial enough. this solution assumes ID numbers are unique.
 

THBMan

Ars Legatus Legionis
18,256
Subscriptor
No need for it to be messy.

code:<HR>
Code:
#!/usr/local/bin/perl<P>use strict;<P>while (<>) # specify the file's name on the command line
{
    chomp;
    my ($name, $email, $id) = split "|";
    my %data{$id} = "$name|$email";
}<P>my $x = 0;
foreach (sort keys %data)
{
    $x++; last if $x == 10;
    print "$data{$_}|$_\n";
}<P>

With the optional line of "$id = sprintf("%08d",$id);" before you put the ID into the hash, depending on how the id numbers are stored. This would pad it out to 8 digits, with leading zeros.

And redirect to a file, it's prettier. While idioms should be respected and learned, there's a lot ot be said for keeping code readable.

execute with: perl idfinder.pl infile.txt > outfile.txt

<div align="right">The Happy Blues Man  </div>
 

THBMan

Ars Legatus Legionis
18,256
Subscriptor
Ok, jemfinch... you seriously need to STOP being logical for just one god damned second! :D

That would be the smart thing to do, but what if all the data is already coded? huh? What would you do then, Mr. Smarty Pants? (and no, the solution of "Reorder each line and then sort is notacceptable! :D )

<div align="right">The Happy Blues Man   </div>
 

Zen Shepherd

Ars Tribunus Angusticlavius
6,877
Subscriptor
THBMan - I think my code is not terribly obtuse.

my ($name, $email, $id) = split "|";
my %data{$id} = "$name|$email";

You do realize both of these lines won't work right? | is for 'or'

split '\|' would work, or /\|/, as I have.

Your hash declaration is also improper. . .you need to my the hash outside the loop, then assign to it with $data{$id} = "whatever"

foreach (sort keys %data)

This also won't do what he wants. . .that will do an ascending string sort, not numerically descending sorting.
 

THBMan

Ars Legatus Legionis
18,256
Subscriptor
THBMan - I think my code is not terribly obtuse.

No, you're right... but hell, perl is a hack language anyway, so code it however you want. I just like my code to look as clean as possible. :D

Quote: "
my ($name, $email, $id) = split "|";
my %data{$id} = "$name|$email";
"

You do realize both of these lines won't work right? | is for 'or'

split '\|' would work, or /\|/, as I have.

Actually, my assignment is correct (worked fine from the command line), but the split is not. I forgot | was the bitwise or.

Your hash declaration is also improper. . .you need to my the hash outside the loop, then assign to it with $data{$id} = "whatever"

Hrm, yes... I do believe I meant to separate that. Nuts. Oh well.

Quote: "
foreach (sort keys %data)
"

This also won't do what he wants. . .that will do an ascending string sort, not numerically descending sorting.

Damn you for destroying my quick 2 minute hack job! :D

But anyway, I'd rather use "foreach (reverse sort keys %data)" and the sprintf I mentioned as optional... but that's just me.

And jemfinch, didn't I tell you to not be logical? :D

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