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

Learn Data Structures easy

by l.frankline (Hermit)
on Feb 06, 2006 at 06:26 UTC ( [id://528164]=perlmeditation: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: Learn Data Structures easy
by brian_d_foy (Abbot) on Feb 06, 2006 at 18:55 UTC

    This is also in the perldsc (dsc = Data Structures Cookbook) documentation. :)

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: Learn Data Structures easy
by zshzn (Hermit) on Feb 06, 2006 at 17:13 UTC
    Neat examples. It could be worth pointing out that data structures can be further nested. Also, usually it would be more applicable to use arrays over hash structures when wanting to index by number.

    s/easy/easily/g;

      Also, usually it would be more applicable to use arrays over hash structures when wanting to index by number.

      Only when the numbers being stored are densely distributed and can be offset into 0..N, when the numbers are sparsely distributed a hash is better.

      ---
      $world=~s/war/peace/g

Re: Learn Data Structures easy
by talexb (Chancellor) on Feb 07, 2006 at 12:19 UTC
      Learn Data Structures easy

    Umm .. perhaps you meant easily?

    Alternatively, Learning Data Structures is Easy!

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Learn Data Structures easy
by EvanCarroll (Chaplain) on Feb 15, 2006 at 01:14 UTC
    For educational purposes:

    Pseudo-Hash:

    A super-fast array that can also be dereferenced as semi-fast hash!

    my $pseudo_hash = [ {foo=>1=>bar=>2=>baz=>3}, baz => bar => foo ]; print $pseudo_hash->{baz}, $pseudo_hash->[2], $pseudo_hash->{foo};

    Use liberally!



    Evan Carroll
    www.EvanCarroll.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://528164]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-25 17:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found