snafu has asked for the wisdom of the Perl Monks concerning the following question:
Merlin helped me some with a paper he wrote for Linux Magazine. However, conceptualization is different from excersization. (:
The objective of this code is to get the number of elements in @var from within multiple sub routines. Is this code optimized or should the use of references make it work more optimally?
The purpose behind this type of example is to keep from repeating lines (in each subroutine) to tell me what the number of elements are in @var. This is why I use subroutines so that I don't have to repeat the same code over and over again.
TIA guys.#!/usr/local/bin/perl -w use strict; &sub1; &sub2; sub sub1 { my @var; my $num; my $numElements; for $num (0..2) { $var[$num] = "blah"; } $numElements = @var; print "Number of elements in \@var of ".(caller(0))[3]." is $numEl +ements\n"; print &eval_array_size(@var),"\n"; } sub sub2 { my @var; my $num; my $numElements; for $num (0..5) { $var[$num] = "blah"; } $numElements = @var; print "Number of elements in \@var of ".(caller(0))[3]." is $numEl +ements\n"; print &eval_array_size(@var),"\n"; } sub eval_array_size { my $numElements = @_; return($numElements); }
----------
- Jim
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(jeffa) Re: Change this example to use references...
by jeffa (Bishop) on Aug 10, 2001 at 01:25 UTC | |
|
Re: Change this example to use references...
by kjherron (Pilgrim) on Aug 14, 2001 at 00:45 UTC |