[pm-h] substitution with increment

Russell L. Harris rlharris at oplink.net
Tue Aug 21 20:24:08 PDT 2007


* G. Wade Johnson <gwadej at anomaly.org> [070820 19:59]:
> Constructing the file names looks like a job for sprintf.

Thanks.  I noticed print and sprint when I read "Learning Perl", but
until now I had no concept of the versatility of the utility.



> There's a great piece of code in the Perl Cookbook ...

I should have known I would reget ordering the seventh volume of Harry
Potter rather than the cookbook.  OK; the cookbook is back at the top
of my "books to purchase" list.



> ... the loop below.
> 
> my $counter = 1;
> foreach $file (@ARGV)
> {
>     my $newname = sprintf( 'Vacation2007-%04d.jpg', $counter );
>     if(!rename( $file, $newname ))
>     {
>         warn "Failed to rename $file to $newname\n";
>     }
> }

The most prominent gap in my understanding of Perl is how to apply
Perl code such as this to a file or to sets of files.  This includes
my comprehension of the diamond operator, which is minimal.  But I
suppose that the Perl Cookbook is the best way to learn the techniques.



> Hope that helps.

Yes; thanks.

RLH



More information about the Houston mailing list