#!/usr/bin/perl -w # # rev-sort -- 1999-04-19 # # inspired by a question on #perl, this program sorts a list of "file # revisions". # use strict; sub rev_sort { # split up the keys on ".": my @ar = split /\./, $a; my @br = split /\./, $b; # these need to be in the outer scope, since we're going to # compare them after we leave the "for" loop my ($xa, $xb) = (0, 0); # loop while we still have values, and while the current values # are still equal. while (@ar && @br && $xa == $xb) { # get the appropriate values from each array $xa = @ar ? shift @ar : -1; $xb = @br ? shift @br : -1; } # since these were the first unequal ones (or, if they're # identical, we ran off the end of the loop), the comparison # between these two is the appropriate one. $xa <=> $xb || @ar <=> @br; } # read in sample data my @test = ; chomp @test; $" = ", "; # man perlvar ... put a comma when we expand arrays in # strings. print "before: [@test]\n"; my @sorted = sort rev_sort @test; print "after: [@sorted]\n"; exit; __END__ 1.0 1.1 6.12 10.4 10.0.3 1.1.1 1.1.2 2.3 1.3 1.4 3.1 3.2 4.5 1.5 1.5.0 1.5.2