Sorry for the bad title but this is the best I could do! 😀
I have a script which creates a new project every time the specified function is called.
Each project must be stored in its own folder, with the name of the project. But, if you don’t specify a name, the script will just name it “new projectX”, where X is a progressive number.
With time the user could rename the folders or delete some, so every time the script runs, it checks for the smallest number available (not used by another folder) and creates the relevant folder.
Now I managed to make a program which I think works as wanted, but I would like to hear from you if it’s OK or there’s something wrong which I’m unable to spot, given my inexperience with the language.
while ( defined( $file = readdir $projects_dir ) )
{
# check for files whose name start with "new project"
if ( $file =~ m/^new project/i )
{
push( @files, $file );
}
}
# remove letters from filenames, only the number is left
foreach $file ( @files )
{
$file =~ s/[a-z]//ig;
}
@files = sort { $a <=> $b } @files;
# find the smallest number available
my $smallest_number = 0;
foreach $file ( @files )
{
if ( $smallest_number != $file )
{
last;
}
$smallest_number += 1;
}
print "Smallest number is $smallest_number";
I think I would use something like:
Test code: