Hamish Whittal said:
>
> I have a reference to an array $ref_arr
>
> I want the length of the array. normally just getting the length one
> would write something along the lines of:
>       $length = @#array; # Unless I am mistaken.

Close -- you want $#array instead.

>
> How do I find the length if I have a reference?
>

You can get the index of the last element with $#$ref_arr, and the length
with scalar(@$ref_arr).  Try this code:

#! perl -w

use strict;

my @array = ( 0, 1, 1, 2, 3, 5, 8 );

my $ref_arr = \@array;

print
  "For array \@array:\n",
  'Length of array:       ', scalar( @array ) , "\n",
  'Index of last element: ', $#array          , "\n",
  "\n",
  "For array referenced via \$ref_arr:\n",
  'Length of array:       ' , scalar( @$ref_arr ) , "\n",
  'Index of last element: ' , $#$ref_arr          , "\n",
  "\n";

__END__


--Bill



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to