πŸ“˜ Incrementing filenames using Perl 6

πŸ“˜ Incrementing filenames using Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.


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

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code