There are a a lot of bugs in your example code. Please add 'use warnings;' at the beginning, you will find out that a lot of errors get noticed that way. Also 'use strict;' is advisable

To answer your question, the problem is that your array references always point to the same data structure. The reason is that you use global variables. To correct that you could either use 'my' to create a new variable every time, i.e. my @array1= ();, or you could make sure that you copy the values, i.e. $hash{$_} = [ [@array1], ... ];

But your script has more problems than that. For example:

$array1=();. The $ should be a @, if you had warnings on, perl would have told you

array1_ref = \@array1;. A '$' is missing in front of the array1_ref. Again perl would have told you with warnings on

Also the clearing of @array1 and @array2 should be before the while loop, if I get your intention right

You could add a line 'use Data::Dumper;' to the beginning of your program and then for example print Dumper(\%hash); at the end to find out how the data structure looks in reality. Data::Dumper ist the best available debugging tool you can find in perl on short notice ;-)


In reply to Re: saving nested arrays in a hash by jethro
in thread saving nested arrays in a hash by Anonymous Monk

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.