in reply to unique filename

Hi lax

Have a look at Auto-increment and Auto-decrement

In particular: "The auto-increment operator has a little extra builtin magic to it."

Combined with neversaint's suggestion you may be able to dispense with the $i altogether.

As Dolly Parton famously said about looking at the docs: "That's what they're for!". Although she might not have been talking about the docs. :-)

Update

See my reply to Skeeve below.

Replies are listed 'Best First'.
Re^2: unique filename
by Skeeve (Parson) on Jul 21, 2006 at 08:13 UTC

    Hi wfsp!

    Can you please give an example how to get rid of the $i here? I always wanted that but never found a way to get filenames incremented without a temporary variable.

    perl -e '$fname= "testname_1";print "$fname\n",++$fname,"\n"'
    prints
    testane_1
    1
    Not a useable result...

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

      The "magic" alphabetic increment doesn't work when there is a non (Update: ASCII it appears ) alphanumeric character in there such as the '_' in this case.

      /J\

      Skeeve

      I should have tested before I posted :-)

      The 'magic' can be useful but with caveats. The following snippet shows when it does work (the first three) and when it doesn't (the final three).

      #!/usr/bin/perl use strict; use warnings; my @array = qw(a123 abc abc1 123a 123_1 abc_1); for my $element (@array){ print "before: $element\n"; $element++; print "after: $element\n"; print "\n"; } __DATA__ output (with comments) # these "do what you want" before: a123 after: a124 before: abc after: abd before: abc1 after: abc2 # and these don't :-( # if the string starts with a number # Perl treats it as number before: 123a after: 124 # the _ always breaks the magic before: 123_1 after: 124 # not at all sure what's happening here! before: abc_1 after: 1