#!/usr/bin/perl -w
use strict;
use warnings;
my @array = qw( a b c d e f g h );
# Code sample 1 -- for simplicity, just display each array item:
foreach (@array) {
printf "%s\n", $_;
}
####
# Code sample 2 -- Come to think of it, I want the indices too ...
for (my $i = 0; $i < @array; $i++) {
my $item = $array[$i];
printf "%3d. $item\n", $i + 1;
}
####
# Code sample 3 -- minor changes from the original "foreach"
# loop (Code sample 1)
foreach (@array) {
printf "%3d. %s\n", $loopcount + 1, $_;
}
####
$# The output format for printed numbers. This variable
is a half-hearted attempt to emulate awk's OFMT variable.
There are times, however, when awk and Perl have differing
notions of what counts as numeric. The initial value is
"%.ng", where n is the value of the macro DBL_DIG from
your system’s float.h. This is different from awk's
default OFMT setting of "%.6g", so you need to set $#
explicitly to get awk's value. (Mnemonic: # is the
number sign.)
Use of $# is deprecated.