Miha Radej wrote:
I don't get why they'd start a sequence halfway through the alphabet... I assume that there is never more than 23 parts to the file (22 alpha's + the base).


They have a script which scans the files periodically and usually just leaves the last few versions of their projects on the disks.


Ah, ok, sound like OpenVMS file versioning. Yes, I can sympathise. When you're finished with the big stick, may I borrow it?

If it's any consolation we have a file versioning system that starts with FILENAME.EXT and goes to FILENAM1.EXT .. FILENAM9.EXT and then moves left a character with FILENA10.EXT .. FILENA99.EXT. If you already have a FILENAM9.EXT that get's overwritten!


First I take a look at the versions and if both versions "a" and "z" exist in an array of files. If so, I use this sort:

@newlist = sort round_robin_sort @filelist;

# use only if both a and z versions are found
sub round_robin_sort
{
    # drop extension
    @alist = split (/\./, $a);
    @blist = split (/\./, $b);
# drop the extension
    pop @alist;
    pop @blist;
$aver = chop ($alist[$#alist]);
    $bver = chop ($blist[$#blist]);
# if versions are from different halves of the alphabet
    elsif (($aver =~ /[a-k]/) && ($bver =~ /[l-z]/)) { return 1; }
    elsif (($bver =~ /[a-k]/) && ($aver =~ /[l-z]/)) { return -1; }
    # if both are from first or last half of the alphabet, just cmp
    else { return $aver cmp $bver; }
}

shortening it up a bit and making it run like a CASE statement:

sub round_robin_sort{
  #assign value of math without extra step
  #then return if pattern match fails
  return -1 unless($aver)=($a =~/([a-z])\./);
  return 1 unless($bver)=($b =~/([a=z])\./);
  return 1 if ($aver =~ /[a-k]/) && ($bver =~ /[l-z]/);
  return -1 if ($bver =~ /[a-k]/) && ($aver =~ /[l-z]/);
  return $aver cmp $bver;
}

OK, so with file system maintenance, you never have so many versions that they span from z..m. That makes sense. And I suppose you don't have the gap problem with a shorter list and your two halves solution.

BTW, thanks for the problem, I'm keeping a stack of really odd problems like this to use when we interview programmers for projects. It's good to see how they approach problems that are more unusual than they get in text books.

Steve
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to