I think that the name of the perl data types: ARRAY and HASH are confusing this issue. It is important not to confuse these data types with the data structures with simular names. Both perl data types can be used to create arrays:
$ARRAY->[1][2] = 'bob';
$HASH->{1}{2} = 'bob';

As this table shows about the only thing you can use a HASH data type for directly is as an array.

data typeARRAYHASH
arrayXX
listX
iteratorX
stackX
queueX

So should you use and HASH or an ARRAY to store arrays? Accessing the ARRAY data type is only slightly faster than accessing the HASH data type, so I would recomend using HASH arrays except for rare cases were speed is vital. If you your arrays are more more 1 dimentional then the ARRAY data type really looses all of its advantage over the HASH data type, as it can no longer be used directly as a list.

         Rate  HASH ARRAY
HASH  54856/s    --   -5%
ARRAY 57536/s    5%    --
And of course every thing else is in an ARRAY (or a SCALAR).
use Benchmark qw (cmpthese); use constant { SEC => 0, MIN => 1, HOUR => 2, MDAY => 3, MON => 4, YEAR => 5, WDAY => 6, YDAY => 7, ISDST => 8, }; our $array = [ qw (sec min hour mday mon year wday yday isdst ) ]; our $hash = { qw (SEC sec MIN min HOUR hour MDAY mday MON mon YEAR year WDAY wday YDAY yday ISDST isdst ) }; sub array { my $a; $a |= $array->[SEC]; $a |= $array->[MIN]; $a |= $array->[HOUR]; $a |= $array->[MDAY]; $a |= $array->[MON]; $a |= $array->[YEAR]; $a |= $array->[WDAY]; $a |= $array->[YDAY]; $a |= $array->[ISDST]; $a; } sub hash { my $a; $a |= $hash->{'SEC'}; $a |= $hash->{'MIN'}; $a |= $hash->{'HOUR'}; $a |= $hash->{'MDAY'}; $a |= $hash->{'MON'}; $a |= $hash->{'YEAR'}; $a |= $hash->{'WDAY'}; $a |= $hash->{'YDAY'}; $a |= $hash->{'ISDST'}; $a; } cmpthese(-1, { 'ARRAY' => \&array, 'HASH' => \&hash, }); cmpthese(-1, { 'ARRAY' => sub { sort @$array }, 'HASH' => sub { sort values %$hash }, });
-- gam3
A picture is worth a thousand words, but takes 200K.

In reply to Re: What defines the choice of a Hash or an Array? by gam3
in thread What defines the choice of a Hash or an Array? by ghenry

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.