Re: store Array in hash ?



On Wed, 2006-29-03 at 23:45 -0800, John W. Krahn wrote:
Michael Gale wrote:
Hello,

Hello,

I have setup a hash like the following:

my $test;

$test->{$setup}->{'opt'} = "OK";

Or:

my $test = { $setup => { opt => 'OK' } };

Or:
my $test{$setup}{opt} = 'OK';


It works fine, now I want to save an array to the hash:

my @data;

push(@data,"test");

$test->{$setup}->{'data'} = @data;

That should be:

$test->{ $setup }{ data } = [ @data ];

Or:

@{ $test->{ $setup }{ data } } = @data;

Be aware that these two statements do a top-level copy of @data:
$test{$setup}{data} = [ @data ];
@{ $test{$setup}{data} } = @data;

Whereas this one stores a reference to @data:
$test{$setup}{data} = \@data;

The difference is that in the first two, if @data changes,
$test{$setup}{data} does not. In the second, it will. Consider what
effect you want in your program when you choose between them.

--
__END__

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them,
we learn by doing them."
Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



.



Relevant Pages

  • Re: beginner help
    ... Just my 0.00000002 million dollars worth, ... Aristotle ... Perl tutorials at http://perlmonks.org/?node=Tutorials ... A searchable perldoc is at http://perldoc.perl.org/ ...
    (perl.beginners)
  • Re: UTF-8
    ... Just my 0.00000002 million dollars worth, ... Aristotle ... Perl tutorials at http://perlmonks.org/?node=Tutorials ... A searchable perldoc is at http://perldoc.perl.org/ ...
    (perl.beginners)
  • Re: generate list in increments of 10s
    ... Just my 0.00000002 million dollars worth, ... Aristotle ... Perl tutorials at http://perlmonks.org/?node=Tutorials ... A searchable perldoc is at http://perldoc.perl.org/ ...
    (perl.beginners)
  • Re: scoping problem with hash
    ... print STDERR Dumper(\%times); ... Just my 0.00000002 million dollars worth, ... Perl tutorials at http://perlmonks.org/?node=Tutorials ... A searchable perldoc is at http://perldoc.perl.org/ ...
    (perl.beginners)