πŸ“˜ Incrementing filenames using Perl 6

Generate a list of filenames like file1.txt, file2.txt, etc.

Perl 6 allows incrementing those kinds of filenames directly:

my $filename = 'file0.txt';
for 1..5 {
   $filename++;
    say $filename;
}

This program prints the list of consequent filenames:

file1.txt
file2.txt
file3.txt
file4.txt
file5.txt

Notice that after reaching 9, the letter from file is incremented. Thus, file9.txt is followed by filf0.txt. To prevent that, add enough zeros in the template:

my $filename = 'file000.txt';
for 1..500 {
   $filename++;
    say $filename;
}

Now, the sequence starts with file001.txt and continues to file500.txt. Multiple file extensions in the template, say file000.tar.gz, are also handled properly, so the numeric part is incremented.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s