Skip to content

Instantly share code, notes, and snippets.

@tacitochaves
Created July 2, 2014 22:03
Show Gist options
  • Select an option

  • Save tacitochaves/45aaac7c6ca64af04503 to your computer and use it in GitHub Desktop.

Select an option

Save tacitochaves/45aaac7c6ca64af04503 to your computer and use it in GitHub Desktop.
Version of file
use strict;
my @list = qw/
nome
nome.1
nome.2
outronome
outronome.2
outronome.3
outronome.10
novo-arquivo
/;
my @cur_files;
my $file_vs_version = {};
foreach my $fn (@list) {
# pega o numero no final, depois do ponto.
my ($name, $version) = $fn =~ /^(.+)\.(\d+)$/;
# se tem numero, eh uma versao, vamos anotar aqui.
if (defined $version){
my $in_list = exists $file_vs_version->{$name};
# ta na lista, mas a versao anotada eh menor que a atual ?
if ($in_list && $file_vs_version->{$name} < $version){
# atualiza a versao.
$file_vs_version->{$name} = $version;
}elsif (!$in_list){
# adiciona a versao na lista.
$file_vs_version->{$name} = $version;
}
}else{
push @cur_files, $fn;
}
}
foreach my $fn (@cur_files){
my $next_version =
exists $file_vs_version->{$fn} # existem varias versoes?
? $file_vs_version->{$fn} + 1 # sim, entao soma um.
: 1; # nao, começa com 1 então.
print ">> fazer backup de '$fn' para cima do arquivo '$fn.$next_version'\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment