Incremental numbers in PerlScript

P

Peter Frank

Hi,

Can someone please tell me how I can get incremental numbers by using
variables in PerlScript, i.e the result should be for example 01, 02,
03, etc.?

Peter
 
J

Jürgen Exner

Peter said:
Can someone please tell me how I can get incremental numbers by using
variables in PerlScript, i.e the result should be for example 01, 02,
03, etc.?

Not sure where your problem is. Many ways, e.g. even a trivial "(1..3)".
If you want to print them in a specific format like e.g. with a leading 0
then check out printf().

jue
 
D

darkon

Peter Frank said:
Can someone please tell me how I can get incremental numbers by
using variables in PerlScript, i.e the result should be for
example 01, 02, 03, etc.?

Here's one way:

for my $n (0 .. 99) {
my $formatted_n = sprintf "%02d", $n;
print $formatted_n, "\n";
}


See the documentation for sprintf for more info. If you just want to
print out the values you might use printf. I used sprintf in case
you wanted to use them internally, say perhaps as part of a hash key.
 
J

John W. Krahn

Peter said:
Can someone please tell me how I can get incremental numbers by using
variables in PerlScript, i.e the result should be for example 01, 02,
03, etc.?

$ perl -le'print for "01" .. "03"'
01
02
03


John
 
T

Tad J McClellan

Peter Frank said:
Hi,

Can someone please tell me how I can get incremental numbers by using
variables in PerlScript, i.e the result should be for example 01, 02,
03, etc.?


perl -le 'print for "01" .. "09"'
 
J

Jürgen Exner

Actually, on second thought, nobody addressed your question.
You specifically asked how to increment numbers that are stored in
variables.

One way to do that is to use the ++ operator, see perldoc perlop. Or you can
just add 1 to the number.

jue
 
P

Peter Frank

Jürgen Exner said:
Not sure where your problem is. Many ways, e.g. even a trivial "(1..3)".
If you want to print them in a specific format like e.g. with a leading 0
then check out printf().

I should have been more specific about what I am trying to achieve.
There is a file renaming tool called awxRename which uses Perl Script
for more complex file renaming purposes.

An example: In order to replace the term "Oldname" in all selected
files with the term "Newname", the perl script command in this program
reads

use locale;$file =~s/Oldname/Newname/g;

The program also contains variables like $name for the current file
name and $ext for the current extension. This allows for example to
replace all current file names (no matter what they are) with a new
term - without modifying the extension:

use locale;$file =~s/$name/Newname$ext/g;

However, since I am not an expert in Perl Script, I don't know how I
would achieve adding incremental numbers to the newly named files. The
program does not appear to offer a variable for incremental numbering.

An example:

Oldname.tif
Othername.tif
File.tif

should be renamed to

Photo_01.tif
Photo_02.tif
Photo_03.tif

There may be many of those files (50 or more), so I wouldn't want to
enter the numbers manually.

Can I somehow do this with Perl Script?

Peter
 
T

Tad J McClellan

Peter Frank said:
Subject: Incremental numbers in PerlScript


PerlScript is not the same as Perl.

This newsgroup is about Perl, so answers will be given as
a "Perl script" rather than as "PerlScript".

An example: In order to replace the term "Oldname" in all selected
files


I think you mean "in all selected filenames" instead.

The name of a file and the contents of a file are not the same either.

with the term "Newname", the perl script command in this program
reads

use locale;$file =~s/Oldname/Newname/g;


That code does not operate on any files at all.

It appears to be operating on the name of a file.

An example:

Oldname.tif
Othername.tif
File.tif

should be renamed to

Photo_01.tif
Photo_02.tif
Photo_03.tif
Can I somehow do this with Perl Script?


I do not know if that can be done with PerlScript.

You certainly can do that with a Perl script though:

-----------------------
#!/usr/bin/perl
use warnings;
use strict;

my $num = '01';
foreach my $old ( qw/Oldname.tif Othername.tif File.tif/ ) {
my($ext) = $old =~ /(\.[^.]+)$/; # m// in list context to grab extension
my $var = "Photo_$num$ext"; # build up the string
$num++; # increment "number"
print "$var\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,206
Messages
2,571,074
Members
47,679
Latest member
Justforamoment

Latest Threads

Top