drodinthe559 has asked for the wisdom of the Perl Monks concerning the following question:

Hey there, is there a reason why the below code DOES NOT work when the variable is defined within loop, but works correctly when a global variable? Also, is there a way to see the renamed file in the array after its been changed?
foreach my $x (@results){ my $ext = "A"; rename ($x->[0], "USB" . $ext); print $x->[0] . "\t" . localtime($x->[1]) . "\n"; ++$ext; }

Replies are listed 'Best First'.
Re: Rename File Name in Array
by rhesa (Vicar) on Jul 20, 2008 at 11:22 UTC
    It "doesn't work", because $ext is reset to "A" on every iteration. You need to declare and initialize that variable outside the loop.

    To update $x->[0] after the rename, simply do $x->[0] = "USB" . $ext;.

      Thanks, I knew it was something cheesy.
        Is there anyway to report that the renaming was successful?