Re: question about data structures - what does $# mean?



On Aug 30, 5:21 pm, Ed <edwardjameshartn...@xxxxxxxxx> wrote:
Howdy all!

Here's a little program:

#!/usr/bin/perl -w
my $d = {sid=>["lll"]};
print $#{$d->{sid}}."\n";

I expect this to print 1, but it prints 0.

What's up with that?

As I read this, $d is a ref to an anonymous assoc. array, which
contains one pair of values, named sid, with an anonymous array
containing one element: "lll".

So to get the number of elements in the array I try:
$#{$d->{sid}}

But that does not work. It gives me 0.

Meanwhile, this does work:
scalar(@{$d->{sid}})

Which made me realize I don't really know what the # does when used in
$#{$d->{sid}}.

Any help would be appreciated!

Thanks,

Ed
$#array gives the last index of the array not the number of elements
in the array, so you need to add 1 to $#array to get the number of
elements.
Since you have only one element its giving 0 , if you put 3 elements
in array it will give 2
.



Relevant Pages

  • question about data structures - what does $# mean?
    ... Howdy all! ... $d is a ref to an anonymous assoc. ... contains one pair of values, named sid, with an anonymous array ...
    (comp.lang.perl.misc)
  • Re: extract matching vales
    ... Ref no spaces in list & unique ... Name No spaces in list but has duplicates but not within the same package ref ... I used a "psuedo" error trap that effectively does the same thing but is ... This will return an array of TRUE's or FALSE's. ...
    (microsoft.public.excel.misc)
  • Re: VBA Efficiency Question
    ... to a 2D array -- the easiest way would be to declare Ref as Refas ... It searches an array about ... Dim ValidRows() As Long, ValidColsAs Long, _ ... > Function TableSum(ByVal RowValue, ByVal ColumnValue, Ref As Range) As Double ...
    (microsoft.public.excel.programming)
  • Re: Control.Invoke and ref parameter
    ... array itself or to the elements in the array? ... If you are marking the object array with "ref", ... If you are marking the elements in the array with "ref", ... "ref" is only valid in method parameter), so we have no option of marking ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: extract matching vales
    ... If you have dynamic ranges that use the OFFSET function then they won't work ... in other calling workbooks unless the source workbook is open. ... Ref no spaces in list & unique ... This will return an array of TRUE's or FALSE's. ...
    (microsoft.public.excel.misc)