Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6529133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:36:04+00:00 2026-05-25T09:36:04+00:00

Sorry for the bad title but this is the best I could do! :D

  • 0

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";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:36:04+00:00Added an answer on May 25, 2026 at 9:36 am

    I think I would use something like:

    use strict;
    use warnings;
    
    sub new_project_dir
    {
        my($base) = @_;
        opendir(my $dh, $base) || die "Failed to open directory $base for reading";
        my $file;
        my @numbers;
        while ($file = readdir $dh)
        {
            $numbers[$1] = 1 if ($file =~ m/^new project(\d+)$/)
        }
        closedir($dh) || die "Failed to close directory $base";
        my $i;
        my $max = $#numbers;
        for ($i = 0; $i < $max; $i++)
        {
            next if (defined $numbers[$i]);
            # Directory did not exist when we scanned the directory
            # But maybe it was created since then!
            my $dir = "new project$i";
            next unless mkdir "$base/$dir";
            return $dir;
        }
        # All numbers from 0..$max were in use...so try adding new numbers...
        while ($i < $max + 100)
        {
            my $dir = "new project$i";
            $i++;
            next unless mkdir "$base/$dir";
            return $dir;
        }
        # Still failed - give in...
        die "Something is amiss - all directories 0..$i in use?";
    }
    

    Test code:

    my $basedir = "base";
    mkdir $basedir unless -d $basedir;
    
    for (my $j = 0; $j < 10; $j++)
    {
        my $dir = new_project_dir($basedir);
        print "Create: $dir\n";
        if ($j % 3 == 2)
        {
            my $k = int($j / 2);
            my $o = "new project$k";
            rmdir "$basedir/$o";
            print "Remove: $o\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the bad title, but I have no idea how to put this
Sorry for the bad title, but... I tried git flow on a project I
Sorry for the bad title, but I don't know how to name this. My
Sorry for bad title. I could not come up with anything better. Every example
Sorry for the bad title, but I couldn't think of a better one. I'm
Sorry for bad english and bad title! I have the table post id title
Sorry for my bad English. Just like what I asked. I have this class
Sorry for the bad title, I wasn't sure what else to call this question.
Sorry for the bad question title. Let's say that I have DateRange class that
Sorry for the vague title, but I wasn't sure how to summarize this in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.