Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Nested (ARRAY|HASH)

by narainhere (Monk)
on Oct 30, 2007 at 06:12 UTC ( [id://647981]=perlquestion: print w/replies, xml ) Need Help??

narainhere has asked for the wisdom of the Perl Monks concerning the following question:

Request fellow monks to have a look at the following code,
use strict; use warnings; my @arr=(12,13,14); my %hash=( "outer"=>( "cool"=>1, "Kewl"=>2,"odd" ) ); print keys %hash,"||",values %hash," ",$hash{"2"};
Though the right way to declare is like
my %hash=( "outer"=>{ "cool"=>1, "Kewl"=>2, } );
I am intrigued after looking at the o/p.How come the supposed to be keys "cool","kewl" became values and values 1,2 became keys.What exactly is the effect of using '()' in-place of '{}'

The world is so big for any individual to conquer

Replies are listed 'Best First'.
Re: Nested (ARRAY|HASH)
by bart (Canon) on Oct 30, 2007 at 06:19 UTC
    You could say the parens are mostly ignored, here (they just influence operator precedence — between only commas, so no effect there), not induce grouping. Perl simply flattens the list. So your first example is equivalent to
    my %hash=( "outer"=> "cool"=>1, "Kewl"=> 2,"odd" );
    which is a fancy way of writing
    my %hash=( "outer", "cool", 1, "Kewl", 2,"odd" );

    You can witness this flattening of the list by passing your construct as parameters to a sub and have it check what it received:

    use Data::Dumper; sub test { print Dumper \@_; } test( "outer"=>( "cool"=>1, "Kewl"=>2,"odd" ) );
    Result:
    $VAR1 = [ 'outer', 'cool', 1, 'Kewl', 2, 'odd' ];
      Many Thanks BART!!

      The world is so big for any individual to conquer

Re: Nested (ARRAY|HASH)
by Zaxo (Archbishop) on Oct 30, 2007 at 09:04 UTC

    Bart is completely correct, but there is an organizing principle he didn't mention. The values in a hash must be scalars. To make a hash of arrays, the array values should have square braces to produce a reference. To make a hash of hashes, curlies.

    Each case is based on the top hash value being a scalar.

    After Compline,
    Zaxo

Re: Nested (ARRAY|HASH)
by naikonta (Curate) on Oct 30, 2007 at 16:51 UTC
    I like how bart explains it, I just wanted to let you know that the B::Deparse module helps to reveal your real structure, unlikely what you want :-)
    $ perl -MO=Deparse 647981.pl use warnings; use strict 'refs'; my(@arr) = (12, 13, 14); my(%hash) = ('outer', ('cool', 1, 'Kewl', 2, 'odd')); print keys %hash, '||', values %hash, ' ', $hash{'2'}; 647981.pl syntax OK

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Nested (ARRAY|HASH)
by jethro (Monsignor) on Oct 30, 2007 at 20:21 UTC
    One small detail should make this quite clear: '=>' is only syntactic sugar (i.e. an alias) for ','.

    Change all '=>' to ',' and it immediately becomes apparent that there is no subhash.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://647981]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-12-05 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (36 votes). Check out past polls.