in reply to Concept map of data types

To my mind, you 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 ->.

For example,

#!/usr/bin/perl use feature "say"; # This is an array of array references: my @array = ( # "(...)" means array [ 1, 2, 3 ], # "[...]" means array reference [ 4, 5, 6 ], ); say $array[1]->[2]; # 6 # firstly, we get the second element from the array # secondly, it's a reference to an array, so we add "->" and get the t +hird element of it # this is a reference to an array of references of arrays my $reference = [ [ 7, 8, 9 ], [ 10, 11, 12 ], ]; say $reference->[0]->[1]; # 8 # this is an array of hash references my @hashes = ( { one => 1, two => 2, three => 3 }, # "{...}" means hash reference { one => 11, two => 22, three => 33 }, ); say $hashes[1]->{one}; # 11
You may also want to use Data::Dumper to look inside your data structures. Perl debugger (perl -d) command "x" is also useful.

Sorry if my advice was wrong.

Replies are listed 'Best First'.
Re^2: Concept map of data types
by programmer.perl (Beadle) on Jul 22, 2012 at 08:34 UTC

    Thanks,

    Here, in your examples I found my mistake, instead of @ I wrote %, I corrected this. Last updated map is above, you can see.

    Yesterday I look for a Data::Dumper in a CPAN but I didn't undesrtand how to use it. I am new in using cpan and modules. I red SYNOPSIS but didn't understand how to use them (code). Do you have any link that help me in this case? In cpan modules till now I could use just Date::Format.

      Data::Dumper can be used to do a lot of interesting things, but basically, you just need to use Data::Dumper and then print Dumper (anything).

      It can be useful to pass a reference to the object you want to see, not just this object. For example, it's useful in case of AoH (array of hashes):

      use Data::Dumper; my @hashes = ( { one => 1, two => 2, three => 3 }, { one => 11, two => 22, three => 33 }, ); print Dumper @hashes; print "-"x5,"\n"; print Dumper \@hashes; __END__ $VAR1 = { 'three' => 3, 'one' => 1, 'two' => 2 }; $VAR2 = { 'three' => 33, 'one' => 11, 'two' => 22 }; ----- $VAR1 = [ { 'three' => 3, 'one' => 1, 'two' => 2 }, { 'three' => 33, 'one' => 11, 'two' => 22 } ];
      ...but it does not improve anything in case of reference to array of references to arrays:
      my $reference = [ [ 7, 8, 9 ], [ 10, 11, 12 ], ]; print Dumper $reference; print Dumper \$reference; $VAR1 = [ [ 7, 8, 9 ], [ 10, 11, 12 ] ]; $VAR1 = \[ [ 7, 8, 9 ], [ 10, 11, 12 ] ];

      Sorry if my advice was wrong.
      Hi,

      You can use Data::Dumper like so:

      use Data::Dumper; my @abc = #abc is an array of references to arrays ( [qw (q w e r)], [qw (1 x 3 r)], [qw (qwe rt 434 )], ); print Dumper ( \@abc ); ## pass array @abc as an array reference my $abc = #abc is an array reference that contains references to ar +rays [ [qw (q w e r)], [qw (1 x 3 r)], [qw (qwe rt 434 )], ]; print Dumper ($abc); ## pass the array reference

      Both print the following:

      $VAR1 = [ [ 'q', 'w', 'e', 'r' ], [ '1', 'x', '3', 'r' ], [ 'qwe', 'rt', '434' ] ];
      Showing 'stringified perl data structures'. It shows you how your data is structured.
      Hope this helps

      Yesterday I look for a Data::Dumper in a CPAN but I didn't undesrtand how to use it.

      Data::Dumper is a core module, so you can view its documentation in perldoc. See especially the EXAMPLES section.

      HTH,

      Athanasius <°(((><contra mundum