tags:

views:

114

answers:

5

How to do a Perl program that contains an array and that array points a hash?

It is like this pictorially,

(M1)        (M2)        ...it goes on
 |--k1=>v1   |--K1=>v1
 |--k2=>v2   |--k2=>v2

I should access that array M1, then the hash it contains inside. (and so on)...

A: 

You need a hash reference, as marked by { } below.

my @array = ({ k1 => "v1", k2 => 'v2' }, { K1 => 'V1', });
Kinopiko
Senthil kumar
+4  A: 

This should do it - though it isn't quite clear to me how you wanted 'M1' and 'M2' to play into the scenario:

my(@array) = ( { k1 => "v1", k2 => "v2" }, { K1 => "V1", K2 => "V2" } );

print "$array[0]->{k1}\n";
print "$array[1]->{K2}\n";

You are making your life more interesting when you use different sets of keys in the different elements of the array (k1 and k2 versus K1 and K2). That's far from forbidden, but it makes the processing harder.

Jonathan Leffler
Jonathan Leffler.. thanks for ur answer
Senthil kumar
Senthil kumar
@Senthil kumar A key cannot contain a hash; a key is a string. Only values can be references to hashes or arrays. If you try to store a reference into a key it will be converted into a string (like `"HASH(0x8d4bc28)"`) that cannot be converted back into a reference.
Chas. Owens
@Senthil `my %hash1 = ( ... ); my %hash2 = ( ... ); my @arr = ({\%hash1 => \%hash2})`. Perl will run this without warning you, but, as Chas says, you can't access the elements of `%hash1` from `@arr`.
flies
+4  A: 

You need to use hash references:

my @array;    
push @array, { k1=>"v1", k2=>"v2" }, { k1=>"v1", k2=>"v2" };

Then, access the hashes like this:

my $val = $array[0]{k1};
eugene y
Interesting: Perl DWIM's both your notation without the arrow and my notation with the arrow (at least, the Perl 5.13.4 that I'm using does).
Jonathan Leffler
@Jonathan Leffler: That's just because when you're already indexing into an array or hash (or array/hash reference), Perl knows that the operator you are trying to index on HAS to be a scalar, and therefore a reference, because only scalars can be inserted into arrays or hashes. So there would be no such thing as a direct list index or hash lookup-- it has to be a reference. HOWEVER, if in the example above you had an `$array_ref` instead of `@array`, then to access it you NEED the first arrow, but not the second: `my $val = $array_ref->[0]{k1};`.
Platinum Azure
@Jonathan: yes, as the perldoc states: "The arrow is optional *between* brackets subscripts."
eugene y
eugene y ... thanks for ur answer this answer suits my requirement
Senthil kumar
Senthil kumar
+1  A: 

Something like:

%h1 = ('a'=>'abc','b'=>'bcd'); # hash 1
%h2 = ('A'=>'Abc','B'=>'Bcd'); # hash 2
@arr = (\%h1,\%h2); # array of hash references.
foreach $hash_ref (@arr) { # iterate through the array.
        foreach $key(keys %$hash_ref) { # iterate through the hash.
                print $key.' '.$$hash_ref{$key}."\n"; #print key => value
        }   
}
codaddict
+1  A: 

In the interests of teaching you to fish, here's a link to the Perl data structures cookbook (perldsc) on building complex data structures in Perl.

davorg