You've got a couple of problems here that are interrelated.

First, each time you execute the statement, $hash{$s++} = \@final; you're using the same array @final...unless @final is a lexical that falls out of scope between each iteration.

Second is a consequence of the first... When you wipe out @final, you're obliterating the content of the array referred to by the hash element you just set.

You should use either (or both) lexical scoping to ensure that @final is a new instance each time you take a reference to it, and/or you should be using the anonymous array constructor to generate a reference to the data held in the array, rather than to the array itself. That means doing:

$hash{$s++} = [ @final ];

Instead of

$hash{$s++} = \@final;

You should definately actively seek to learn and understand lexical scoping as discussed in perlintro and perlsub, as well as the difference between taking a reference, and creating an anonymous array (see perlreftut and perlref, and perllol). I hope this helps...


Dave


In reply to Re: array reference by davido
in thread array reference by prasadbabu

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.