in reply to Use of uninitialized value in print at line 20 (#1)
my @inputArray = (1,2,3,4,5,6,7,8);
my $inputArrayLen = @inputArray;
Even given the recursive context in which the OPed question is couched, I'm a bit surprised that no one has yet mentioned the $# array operator (if that's the correct terminology), which gives you the maximum index of the array. (Update: See perldata for a discussion of $# and arrays.)
As others have pointed out, evaluating an array in scalar context as in the
my $inputArrayLen = @inputArray;
statement yields the number of elements of the array. Using the $# operator, the statement
my $maxIndex = $#inputArray;
gives you the highest index of any element in the array.
Leaving aside any considerations of recursion, the idiomatic, "Perlish" way of iterating over an array is something like
Leaving aside any considerations of recursion, a better way of iterating over an array is something like
my @array = (...);
...
for my $element (0 .. $#array) {
do_something_with($element);
}
my @array = (...);
...
for my $i (0 .. $#array) {
do_something_with($array[$i]);
}
and better yet IMHO, the idiomatic, "Perlish" way would be
for my $element (@array) {
do_something_with($element);
}
Of course, there are other ways, such as using the built-in functions map and grep, of iterating over arrays, but those are other stories for other days.
Update: Fixed the brainfart for-loop example.
Give a man a fish: <%-{-{-{-<
|
|---|