On 07/25/2006 10:07 PM, Mumia W. wrote:
On 07/25/2006 08:32 PM, macromedia wrote:
Hi,
I can't seem to get my script to sort properly. Below is my code [...]
sort { $a->[0] cmp $b->[0] || $a->[7] <=> $b->[7] }
[...]
"Cmp" does string comparisons. Use "<=>" for numeric
comparisons. Read "perldoc perlop".
No, this doesn't quite do it. I see your data is a little more
complicated than what I thought before. When you have unusual
sorting requirements, you need unusual sorting keys. I decided
to use sprintf() to give me keys that are formatted perfectly
for sorting:
require 5.000;
use warnings;
use strict;
use POSIX;
my %tags = ();
my $input = 'sort_tags.dat';
my $output = 'sort_tags.out';
open (FILE, "< $input") or die "cannot open $input: $!\n";
while (my $tag = <FILE>) {
$tag =~ m/<tag id=(\d*)([[:alpha:]]*)>/;
$tags{sprintf("%04d%6s",$1 || 999,$2)} = $tag;
}
open (NEWFILE, "> $output");
foreach my $id ( sort keys %tags )
{
print NEWFILE $tags{$id};
}
close NEWFILE;
close FILE;
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>