Wc -Sx- Jones wrote:
> 
> Michael C. Davis wrote:
> > Hi, is there a good way to tell how much memory a given data structure is
> > consuming?  I realize that there are issues in using this number to
> > determine runtime memory requirements (like the fact that, in some
> > circumstances, a running Perl process does not give back allocated memory
> > to the operating system until the process exits) but ... are there
> > guidelines like 100 bytes per REF, for example?  Or is there a routine to
> > which I can pass my data structure and have it tell me the size?  Thanks!
> 
> There is likely a shoreter trick, but to give
> you an idea path to follow -
> 
> my @array;
> 
> my $size = length @array;
             ^^^^^^^^^^^^^

> print "$size\n\n";
> 
> @array = qw/one two three four/;
> 
> $size = length @array;
          ^^^^^^^^^^^^^

> print "$size\n\n";
> 
> $size = 0;
> 
> foreach (@array) {
>      $size += length;
> }
> 
> print "$size\n\n";

length() evaluates its argument in scalar context and an array in scalar
context returns the number of elements in the array so you are getting
the length of the number of elements.

$ perl -le'
my @array;
print scalar @array, " ", length @array;
@array = 1 .. 99;
print scalar @array, " ", length @array;
'
0 1
99 2



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to