http://www.perlmonks.org?node_id=983006

programmer.perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I'm getting confused on data types, especially on complex data types with a references. To the Perl enthusiast, like me, it take a bit of time to memorize all data combinations in Perl. Therefore, to understand better, I gathered formats of a data types from a book (Perl by Example 4th edit.). It would be better if you look to this with your 'eagle' eye, every-time, Master has something to say to a beginner concerning his works..., maybe this concept map needs to be filled with additional FORMATs that I don't know... I attached the text as a code, otherwise some symbols were interpolating...

Thanks to all monks (especially to 2teez, Marshall, aitap) who helped in redesigning this map. This is a latest view:

---PERL DATA TYPES--- COMPLEX DATA TYPES # We just need to memorize that arrays and hashes can contain # only scalars and references, and in order to use contents of # an object by its reference you need ->. @abc=([],); LIST OF LISTS @abc=({""=>"",},); ARRAY OF HASHES $ref=['',['',]]; LIST OF LISTS: anonymous arrays $ref=['',]; ANONYMOUS ARRAYS $ref=[{""=>"",},]; ANONYMOUS ARRAY OF HASHES %abc=(""=>"",); HASH %abc=(""=>{""=>"",},); HASHES OF HASHES %abc=(""=>["",]); HASHES OF HASHES $ref={""=>"",}; ANONYMOUS HASHES $ref={key=>{""=>"",},}; ANONYMOUS HASH OF HASHES $ref={""=>{""=>['',]},}; HASH OF HASHES WITH LISTS OF VALUES ----------------------------------------------------------- --DETAILS-- SCALAR-$ $abc = "string"; $abc = 12; $abc = $def; ------------------------------------------------------------ ARRAY-@ @abc = "q, w, e, r, t, y"; @abc = (1, 2, 3, 4, 5, 6); ____________________________________________________________ LIST OF LISTS--FORMAT: @abc=([],); my @array = ( # "(...)" means array [ 1, 2, 3 ], # "[...]" means array reference [ 4, 5, 6 ], ); print $array[1]->[2]; # output: 6 # firstly, we get the second element from the array # secondly, it's a reference to an array, so we add "->" and # get the third element of it my @abc = #abc is an array of references to arrays ( [ qw (q w e r) ], [ qw (1 x 3 r) ], [ qw (qwe rt 434 ) ], ); foreach my $array_ref (@abc) { print "@$array_ref \t"; print "third element: \t", $array_ref->[2],"\n"; } __END__ Prints: q w e r third element: e 1 x 3 r third element: 3 qwe rt 434 third element: 434 ____________________________________________________________ ARRAY OF HASHES--FORMAT: @abc=({""=>"",},); @abc = ({"key"=>"val", "key"=>val,},); print $abc[0]->{"key"}; ____________________________________________________________ LIST OF LISTS: anonymous arrays--FORMAT: $ref=['',['',]]; my $ref = ['1', '2', ['a', 'b']]; print $ref->[0]; output: 1 print $ref->[2]->[1]; # output: b print @{$ref}; # output: 1 2 ARRAY(0x8a6f134) print @{$ref-[2]}; # output: a b ____________________________________________________________ ANONYMOUS ARRAYS--FORMAT: $ref=['',]; my $ref = ['a', 'b',]; print "$ref->[0]"; # or $$ref[0] or ${$ref}[0], output: a print "@{$ref}"; # output: a b ____________________________________________________________ ANONYMOUS ARRAY OF HASHES--FORMAT: $ref=[{""=>"",},]; my $ref = [{"keyA"=>"valA",},]; print "$ref->[0]->{keyA}"; # output: valA push @{$ref}, {"keyC"=>"valC",}; while (($k,$v)=each %{$ref->[1]}) {print "$k -- $v";} ------------------------------------------------------------ HASH-% FORMAT: %abc=(""=>"",); %abc=("key1"=>"val1", "key2"=>val2,); print "$abc{'key2'}"; # output: val2 ____________________________________________________________ HASHES OF HASHES A) FORMAT: %abc=(""=>{""=>"",},); %abc = ("key1"=>{"key1-1"=>"valueAA", "key1-2"=>"valueAB"},); print qq/$abc{key1}->{key1-2}/; # output: valueAB B) FORMAT: %abc=(""=>["",]); %abc = ("key"=>["val", vals,]); print "$abc{key}->[1]"; # output: vals ____________________________________________________________ ANONYMOUS HASHES--FORMAT: $ref={""=>"",}; my $ref = {"key"=>"val",}; print $ref->{"key"}; print keys %$ref; # or values %$ref, output: key or val ____________________________________________________________ ANONYMOUS HASH OF HASHES--FORMAT $ref={key=>{""=>"",},}; my $ref = {key1=>{"key"=>"val", "key"=>val,},}; print "$ref->{'key1'}->{'key'}"; $ref->{'key1'}->{'key'}=val; print %{$ref->{'key1'}}; # output: keyvalkeyval foreach $outer_key (keys %{$ref}) { print "Outer key: $outer_key\n"; while (($k,$v)=each(%{$ref->{$k}})) {print "$k: $v"} } ____________________________________________________________ HASH OF HASHES WITH LISTS OF VALUES--FORMAT: $ref={""=>{""=>['',]},}; my $ref = {"key1"=>{"key"=>['a','b',]}, "key2"=>{"key"=>['c','d',]},}; print $ref->{"key1"}->{"key"}->[0]; # output: a print "@{$ref->{'key2'}->{'key'}}"; # output: c d push @{$ref->{'key1'}->{'key2'}}, "@{$adds}"; # anonymous array: $adds = ["e","f"]; print "@{$ref->{'key2'}->{'key'}}"; # output: c d e f ------------------------------------- # "[ ITEMS ]" makes a new, anonymous array, and returns a # reference to that array. "{ ITEMS }" makes a new, anonymous # hash, and returns a reference to that hash. # References are documented in perlref and perlreftut. # Examples of complex data structures are given in perldsc and perllol # Examples of structures and object-oriented classes are in perltoot.