Data Structures

The Data Structures are of a bit complex to understand but it very powerful in storing huge records. Have a look at the data structures explained very easy.

Arrays of Arrays

There are many kinds of nested data structures. The simplest kind to build is an array of arrays, also called a two-dimensional array or a matrix.

@array = ( ['a','b'], ['c','d'], ['e','f'] ); print $array[1][0]; #result c;

Arrays of Hashes

An array of hashes is useful when you have a bunch of records that you'd like to access sequentially, and each record itself contains key/value pairs.

@array = ( { 'a'=>'b', 'b'=>'c' }, { 'c'=>'d', 'd'=>'e' }, { 'e'=>'f', 'f'=>'g' } ); print $array[1]{c}; #result d;

Hashes of Arrays

Use a hash of arrays when you want to look up each array by a particular string rather than merely by an index number.

%hash = ( "1" => ['a','b'], "2" => ['c','d'], "3" => ['e','f'], ); print $hash{1}[0]; result a

Hashes of Hashes

A multidimensional hash is the most flexible of Perl's nested structures. It's like building up a record that itself contains other records.

%hash = ( "1" => { 'a'=>'b', 'b'=>'c' }, "2" => { 'c'=>'d', 'd'=>'e' }, "3" => { 'e'=>'f', 'f'=>'g' } ); print $hash{2}{d}; #result e

Moved from Q & A to Meditations by Arunbear


In reply to Learn Data Structures easy by l.frankline

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.