Perl For
Perl like almost all languages have provision for loop. Per for loop is used to loop through a designated block of code until a specific condition is met.
As a simple example look below:
for ($count = 10; $count >= 1; $count--) {
print "$count ";
}
print "End"; This Perl script has the following output:
10 9 8 7 6 5 4 3 2 1 End
More complex example for Perl for loop is seen in next example.
Perl For Example: Renaming Files
The code below can be used to rename series of files that start with the same number but ends on different extention (frames). Notice how it is iterating in the current file listing between start and stop in the perl for loop. start means which file number to start from and stop means how many files.
#!/usr/local/bin/perl
#
# rename series of frames
#
if ($#ARGV != 3) {
print "usage: rename old new start stop\n";
exit;
}
$old = $ARGV[0];
$new = $ARGV[1];
$start = $ARGV[2];
$stop = $ARGV[3];
for ($i=$start; $i <= $stop; $i++) {
$num = $i;
if($i<10) { $num = "00$i"; }
elsif($i<100) { $num = "0$i"; }
$cmd = "mv $old.$num $new.$num";
print $cmd."\n";
if(system($cmd)) { print "rename failed\n"; }
}Some of the other details is here: Perl for loop