Dear monks, I am having difficulty refreshing myself on referencing multidimensional arrays, I am sure I knew this before but it eludes me now. Whether my approach to this is just nonsense or whether its OK but my referencing is wrong I am not sure but I am pretty sure I have done this in the past. My code is this:
use strict; my @myarr; my $lpn; my $ppn; my $i; for ($i=0;$i<15;$i++) { ($lpn, $ppn) = get_ns($i); push @{ $myarr[$lpn] }, $ppn; } for my $index (0..$#myarr) { next unless defined $myarr[$index]; my @tmp; for my $subindex (0..scalar(@{$myarr[$index]})-1) { my $date; for ($date=0;$date<10;$date++) { $tmp[$date] = get_date($index, $subindex); } push @{$myarr[$index][$subindex]}, [ @tmp ]; } } sub get_ns { return (int(rand(100)), int(rand(100))); } sub get_date { return get_ns; }
I get
Can't use string ("23") as an ARRAY ref while "strict refs" in use at +./eg.pl line 24.
when I try to run it, and my line
push @{$myarr[$index][$subindex]}, [ @tmp ];

seems to be the problem - I've tried all the variations on syntax I can remember trying to hit on the correct one and searched perlmonks for similar questions but I can't find examples with 3 levels of array and moving from 2 to 3 seems to be my problem. However perhaps I am not seeing that somewhere I am making a mistake higher up and that I am indeed referencing it right but not stuffing the right values and hence causing the error, I am not sure.

If anyone can point out my error it would be hugely appreciated.

Pete


In reply to Adding cols to 3d arrays - syntax by peterrowse

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.