On 08/21/2007 07:41 AM, Tony Heal wrote:
the list is a list of files by version. I need to keep the last 5 versions.
Jeff's code works fine except I am getting some empty strings at the beginning
that I have not figured out.
Here is what I have so far. Lines 34 and 39 are provide a print out for
troubleshooting. Once I get this fixed all I
need to do is shift the top five from the list and unlink the rest.
#!/usr/bin/perl
use warnings;
use strict;
opendir (REPOSITORY, '/usr/local/repository/dists/');
my @repositories = readdir (REPOSITORY);
closedir (REPOSITORY);
my $packageRepo;
my @values;
my @newValues;
foreach (@repositories)
{
$packageRepo = $_;
chomp ($packageRepo);
opendir (packageREPO,
"/usr/local/repository/dists/$packageRepo/non-free/binary-i386");
my @repoFiles = readdir (packageREPO);
close (packageREPO);
foreach (@repoFiles)
{
my $fileName = $_;
chomp ($fileName);
if ( /(.*)(([0-9][0-9])(-special)?\.([0-9])(-)([0-9]*))(.*)/)
{
push (@values, $2);
}
}
my %h;
foreach (@values)
{
push (@newValues, $_) unless $h{$_}++
}
foreach (@newValues){print "$_\n";}
my @new = map { $_->[0] }
sort { $b->[1] <=> $a->[1] }
map { [$_,(split/-/)[-1]] }
@newValues;
print "@new[0..4]\n";
}
Or for a line numbered version
http://rafb.net/p/asqgJo27.html
Tony Heal
[oops, sent to the wrong list before]
Sort::Maker should make short work for this task ;-)
All you have to do is to make a regex to pull out the version numbers.
After that, you're practically done:
use strict;
use warnings;
require Sort::Maker;
open (pkgREPO, '<', 'data/versions-list.txt')
or die "no versions list: $!";
my @versions;
while (my $line = <pkgREPO>) {
chomp $line;
push @versions, [ $line, $line =~ /^(\d+)(?:-[a-z]+)?\.(\d+)-(\d+)/ ];
}
close pkgREPO;
my $sorter = Sort::Maker::make_sorter(
'ST',
number => '$_->[1]',
number => '$_->[2]',
number => '$_->[3]',
);
die $@ unless $sorter;
my @sorted = $sorter->(@versions);
print "keep: $_->[0]\n" for @sorted[$#sorted-4 .. $#sorted];
print "delete: $_->[0]\n" for @sorted[0 .. $#sorted-5];
__END__
This is the output:
keep: 16.5-2
keep: 16-special.5-2
keep: 16.5-10
keep: 16.5-13
keep: 16-special.6-6
delete: 14-special.1-2
delete: 14-special.1-8
delete: 14-special.1-15
delete: 14-special.2-40
delete: 14-special.2-41
delete: 14-special.3-4
delete: 14-special.3-7
delete: 14-special.3-12
delete: 15-special.1-52
delete: 15-special.1-53
delete: 15-special.1-54
delete: 15.2-108
delete: 15.2-110
delete: 15.2-111
delete: 15.3-12
delete: 16.1-17
delete: 16.1-22
delete: 16.1-23
delete: 16.1-39
delete: 16.3-1
delete: 16.3-6
delete: 16.3-7
delete: 16.3-8
delete: 16.3-15
delete: 16-special.4-9
delete: 16-special.4-10
delete: 16.5-1
delete: 16-special.5-1
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/