Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Rename File Name in Array

by drodinthe559 (Monk)
on Jul 20, 2008 at 11:17 UTC ( [id://698910]=perlquestion: print w/replies, xml ) Need Help??

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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://698910]
Approved by Krambambuli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-19 03:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found