You have to pay attention to the sigils to understand why your method isn't working as you hoped. If the variable entity starts with a $ sigil, it's a scalar container. If it starts with a @ sigil, it's an array (a list container). A scalar container cannot hold a list. That means that the following statement behaves as follows:

$arr[0] = @alpha;

Behavior: Look at the container on the left hand side of the = operator. It's a scalar. That means that the right-hand side of the statement is evaluated in scalar context. An array, evaluated in scalar context doesn't return its contents, but rather, returns the number of elements it contains.

What you want, instead, is to put a reference (which is itself a scalar value) to the array into the scalar container '$arr[0]'. You can do that like this:

$arr[0] = \@alpha;

That puts a "pointer" or more appropriately described as a "reference" to @alpha into @arr[0]

In many situations you might prefer to hold a reference to a copy of @alpha's contents instead of a reference to @alpha itself. You can do that like this:

$arr[0] = [ @alpha ];

And in the name of not typing $arr[0], $arr[1], $arr[2], and so on, you can also do something a little cunning like this:

foreach( \@alpha, \@beta ) { push @arr, $_; }

...or for a copy instead of a direct reference...

foreach( [ @alpha ], [ @beta ] ) { push @arr, $_; }

See perlref, perlreftut, and perllol for a much better explanation than I can give.

Now to determine length, you can do this:

my $len = @arr; # Number of lists contained in @arr. my $len = @{ $arr[0] }; # Number of elements contained in the list # referenced by $arr[0].

The idea is to evaluate the array, or one of the array refs held in the array, in scalar context. Doing so will tell you how many elements that level contains.


Dave


In reply to Re: determining length of multidimensional arrays by davido
in thread determining length of multidimensional arrays by scuffell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.