Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Concept map of data types

by programmer.perl (Beadle)
on Jul 21, 2012 at 19:59 UTC ( #983006=perlquestion: print w/ replies, xml ) Need Help??
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.

Comment on Concept map of data types
Download Code
Re: Concept map of data types
by 2teez (Priest) on Jul 21, 2012 at 20:48 UTC
    Hi,

    Why don't you also take a look at the following, on your Perl documentation:

    • perlref,
    • perldsc,
    • perllol,
    • perldata,

    All you need do is 'perldoc perl....'. These will really help!

      Thanks, I found useful 'perldoc perl...', till time I was searching for any info in the Internet. I added these to my map. Last updated is given above.

Re: Concept map of data types
by Marshall (Prior) on Jul 21, 2012 at 21:12 UTC
    There are many things that are <s>completely/<s> very wrong.
    There are many things that are not right. Let's start with LOL, List of List (Array of Array):
    #!usr/bin/perl -w use strict; #LISTS OF LISTS (array of array) 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
    <Update:>

    Many instructors talk about list of list versus array of array.
    This is not a "terrible thing".
    There is actually a rather fine difference between these two concepts.

      I added your example to the concept map. It is a good another example of array of arrays. Also, I wrote and added this lines to the section 'HASH OF HASHES WITH LISTS OF VALUES'

      push @{$ref->{'key1'}->{'key2'}}, "@{$adds}"; # anonymous array: $adds = ["e","f"]; print "@{$ref->{'key2'}->{'key'}}"; # output: c d e f
      I found useful 'perldoc perl...', till time I was searching for any info in the Internet.
Re: Concept map of data types
by aitap (Deacon) on Jul 22, 2012 at 07:21 UTC

    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.

      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.

        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

        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

Re: Concept map of data types
by cheekuperl (Monk) on Jul 23, 2012 at 07:06 UTC
    I'm getting confused on data types, especially on complex data types with a references.
    Precisely the same question had come to my mind after I confused myself with some random thinking on the subject. Check Question about Perl refs and basic types
    All ye, repeat after me: There are only three data types in Perl - scalars, arrays and hashes - no more, no less.

    To the Perl enthusiast, like me, it take a bit of time to memorize all data combinations in Perl.
    Figuratively speaking, there could be infinite combinations. e.g. Arrays of hashes of arrays of hashes of arrays of arrays :)
    I would rather treat "data types" and "data structures" as two different subjects.
      Yes, "data types" and "data structures" are two different subjects, I will consider this. And, there are only three types of data: scalar, array and hashes, all others are combination of each of others :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://983006]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2014-07-26 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls