If you intend to do it in Perl, then you probably want to create a data structure where each node contains the name of the original file on one hand and the various components of the name on the other hand. Then you can sort on the various parts and store the sorted order into an array which you can then use to figure out what you want to keep live and what you want to set aside.
Step one: splitting the names. Maybe something like this:
It could be done with shorter code, but I preferred to break the process into small parts for better comprehension. Now the records in the @to_be_sorted array look like this:my @to_be_sorted; foreach my $filename (@filelist) { my ($root, $version) = $filename =~ /([a-z]+)_(\d+\.\d+\.\d+)/; my ($major, $minor, $third) = split /\./, $version; push @to_be_sorted, [$filename, $root, $major, $minor, $third]; }
Now you can sort on elements 1, 2, 3 and 4 of each record and store into a new sorted array element 0 of each item. Something like this (not really tested):0 ARRAY(0x600500678) 0 'bar_123.10.0_deb' 1 'bar' 2 123 3 10 4 0
The whole code shown above could be reduced to a single instruction using the clever Schwartzian Transform (see also Efficient sorting using the Schwartzian Transform), but I would not necessarily recommend it in this case, because the initial splitting is a bit tedious.my @sorted_array = map {$_->[0]} sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2] || $a->[3] <=> $b->[3] || $a->[4] <=> $b->[4] } @to_be_sorted;
Please note that I fully agree with the previous post by Anonymous Monk, I have just chosen one plausible way of sorting the version numbers, you may have to change it in accordance to the Debian version number conventions.
In reply to Re: File Parsing
by Laurent_R
in thread File Parsing
by kel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |