If you were using strict, you wouldn't probably wouldn't be having this problem because you would have declared @array with my and made it lexically scoped. As it is, you're using a global variable, thus always pointing to the same reference. However, even lexically scoping the array isn't going to help because you're calling undef on the array that you're pointing to. The following should work. I replaced your for loop with a call to Data::Dumper to make it easier to see the results.

use strict; use Data::Dumper; my %cluster; while (<DATA>) { chomp; my @ary = split; my $user = shift @ary; $cluster{$user} = \@ary; } print Dumper \%cluster; __DATA__ bob's your uncle Ovid is in da house

To get a little more specific, if you use a variable without declaring it (such as when you fail to use strict and my), the variable is automatically created as a global variable in the current namespace. For most programs, this means your variables with be in the main:: namespace. Your @ary is actually @main::ary. Every time you took a reference to it, it was the same array.

When you lexically scope a variable by declaring it with my, you create a new, private instance of that variable that has absolutely no relation to a global variable of the same name. For instance, these two instances of $foo are not the same:

#!/usr/bin/perl $foo = "bar"; { my $foo = "something else"; print "$foo\n"; } print "$foo\n";

Behavior like this can introduce plenty of subtle bugs, if people are unaware of what's going on. By always using strict, you can avoid these bugs.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: Arrary ref stored in hash always points to the same array? by Ovid
in thread Arrary ref stored in hash always points to the same array? 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.