On Tue, 2008-04-08 at 07:06 -0700, [EMAIL PROTECTED] wrote:
> Hello,
> 
> I want to store  a hash into an array: e.g.: like this
> 
> #!/usr/bin/perl -v
> 
> print "Content-type: text/html\n\n";
> 
> $h{'hello'}="hello";
> $h{'by'}="by";
> $a[0]=$h;
> 
> 
> and if i print out the array like this
> 
> print $a[0]{'hello'}." and ".$a[0]{'by'};
> 
> but it shows nothing. can somebody explain me where is a problem???
> thanks.....
> 
> 

What you want to do is store a reference to a hash or store an anonymous
hash.

Storing a reference to a hash
  $a[0] = \%h;

Storing an anonymous hash
  $a[0] = { %h };

The difference can be seen here:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my %h = ();
my @a_ref = ();
my @a_anon = ();

$h{'hello'}="hello";
$h{'by'}="by";
$a_ref[0] = \%h;
$a_anon[0] = { %h };

print "\%h = ", Dumper \%h;
print "[EMAIL PROTECTED] = ", Dumper [EMAIL PROTECTED];
print "[EMAIL PROTECTED] = ", Dumper [EMAIL PROTECTED];

print "\nNow some changes...\n";

$h{'hello'}="bonjour";
$h{'by'}="pres";

print "\%h = ", Dumper \%h;
print "[EMAIL PROTECTED] = ", Dumper [EMAIL PROTECTED];
print "[EMAIL PROTECTED] = ", Dumper [EMAIL PROTECTED];

__END__

-- 
Just my 0.00000002 million dollars worth,
    Shawn

99% of you are just big dumb apes!

+------------\
| Shangri La   \
| 40,000 KM     /
+------------/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to