#!/usr/local/bin/perl
use strict;
my @array = ('one', 'two', 'three');
for my $element (@array) {
$element = uc($element);
}
for my $element (@array) {
print "$element\n";
}
exit;
The output was:
ONE
TWO
THREE
In this regard the for does in fact seem to be synonymous with foreach.
In the extent that the elements of the array are changed
both function the same.
But I must admit that by programming habit and for the
sake of readiblity I adhere to the convention:
foreach loop to change the contents of an array
"for" when I'm not.
In spite of my habits and preferences apparently they both work the same. But alas my C background
has me using for when I'm just stepping through array indexes.
|