develooper Front page | perl.beginners | Postings from September 2009

Re: Hash of Hashes

Thread Previous
From:
Chas. Owens
Date:
September 29, 2009 05:36
Subject:
Re: Hash of Hashes
Message ID:
58ce48dc0909290536m15ca6abdl86f4c8f748db7a94@mail.gmail.com
On Tue, Sep 29, 2009 at 07:40, Jeff Peng <Jeff.Peng@freenet.de> wrote:
> 2009/9/29 Shawn H Corey <shawnhcorey@gmail.com>:
>> Soham Das wrote:
>>>
>>> How can I create a Hash of Hashes from two lists. Is it possible?
>>>
>>> I want the effective functionality to be served like this
>>>
>>> $ChildHash["Joe"]["21A"]="Sally"
>>>
>>> i.e Joe at 21A has a child called Sally. List1 here will be the name of
>>> Parents, List2 here will contain the house number.
>>
>> Hashes use {}, arrays use []
>>
>
>
> That's in Python? :-)
> Perl's both hash and array use ().
> But anonymous hash and array use {} and [].
snip

To clarfy,

Perl uses (LIST) to initialize hashs and arrays.  One way to create a
list is (X).

my @a = (1, 2, 3);
my %h = (a => 1, b => 2, c => 3);

Perl uses X[Y] to get the value at the Yth index in X when X is an
array or a list.

my $x = $a[1]; #$x is now 2

Perl uses X{Y} to get the value associated with Y when X is a hash.

$x = $h{c}; #$x is now 3

Perl uses [LIST] to create an anonymous array ref.

my $aref = [1, 2, 3];
$x = $aref->[0]; #$x is now 1

Perl uses {LIST} to create an anonymous hash ref.

my $href = {a => 1, b => 2, c => 3};
$x = $href->{b}; #$x is now 2

See [perldoc perlop][1] or my [perlopref][2] document for more information.

[1] : http://perldoc.perl.org/functions/perlop.html
[2] : http://github.com/cowens/perlopref

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About