Am Montag, 18. April 2005 10.53 schrieb Vladimir D Belousov:
> Charles K. Clarkson wrote:
> >Aaron C. de Bruyn <mailto:[EMAIL PROTECTED]> wrote:
> >: In the Perl documentation (perlintro) it says that you can find out
> >: the number of elements in an array using the following syntax:
> >:
> >: print $myvar[$#mmyvar];
> >
> >    No. That prints the value of the last element of @myvar. The number

^^^

> >items in the array is the scalar evaluation of @myvar.
> >
> >print scalar @myvar;
>
> I beg your pardon, but why $myvar[$#mmyvar] is the first element of array?

Read the line marked with ^^^^ above again :-)

perl -le '
use strict; use warnings;
my @a=(1,2,3);
print $a[$#a], "\n"; # here the same as $a[2];
'
# prints:

3


>
> For example:
>
> @myvar = qw(a b c d e f g);
>
> and as I can understend, $#myvar is 7, but the last element has index 6.

Yes, because the first array index is 0  and thus the number of elements one 
more 
than the last index.

perl -le'
use strict; use warnings;
my @a=();
my @b=(0);
my @c=(0,1);
print $#a, " / ", $#b, " / ", "\n";
'
# this prints:

-1 /  0 / 1

greetings joe

>
> >HTH,
> >
> >Charles K. Clarkson
>
> --
> Vladimir D Belousov

-- 
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