Andrew Gaffney wrote:
>
> david wrote:
> > Andrew Gaffney wrote:
> >
> >
> >>I've got an array reference and I'm trying to figure out how many elements
> >>are in it. I've tried '$#arrayref', '[EMAIL PROTECTED]', '$(@($arrayref))', and
> >>probably a few others that I've forgotten. What is the correct way to do
> >>this?
> >
> >
> > you can try @{EXP} or $#{EXP}+1 where EXP is your array reference:
> >
> > [panda]# perl -le 'print $#{[1,3,5,7]}+1'
> > 4
> > [panda]# perl -le 'print @{[1,3,5,7]}+0'
> > 4
> > [panda]#
> >
> > the '{}' is sometimes optional depends on EXP, i usually use it for personal
> > perference.
>
> '$#{$arrayref}+1' worked for me.

No! $# is explicitly meant for determining the index of the last element of an
array!

  my $size = @{$arrayref}

conveys much better what you mean. You may need to force scalar
context in something like

  printf "Size = %d\n", scalar @{$arrayref}

but it's still a lot more accessible.

HTH,

Rob



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