in reply to Re: unique filename
in thread unique filename

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

Replies are listed 'Best First'.
Re^3: unique filename
by gellyfish (Monsignor) on Jul 21, 2006 at 08:27 UTC

    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\

Re^3: unique filename
by wfsp (Abbot) on Jul 21, 2006 at 09:14 UTC
    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