http://qs321.pair.com?node_id=1069835


in reply to Sequentially numbering output files rather than overwriting an existing file

For the first unused "$name$n.txt" file:
if (-f "$name.txt") { $n=1; $n++ while -f "$name$n.txt"; }
For one greater than the highest "$name$n.txt" file, something like kcott's solution below works.
  • Comment on Re: Sequentially numbering output files rather than overwriting an existing file
  • Download Code

Replies are listed 'Best First'.
Re^2: Sequentially numbering output files rather than overwriting an existing file
by marinersk (Priest) on Jan 08, 2014 at 18:03 UTC
    For more visually pleasing output:
    if (-f "$name.txt") { $n=1; my $fn = sprintf "%s-%05d\.txt", $name, $n; $n++ while -f $fn; }