IMHO...

1. In general, an array will be more efficient (i.e. quicker to read values from), but only if you know the element number that you are trying to access, or if you know that you want to perform a function on each element of the array - i.e.

#good print $array[3]; # faster than... print $hash{'3'}; #... this #bad for(0..$#array){ if(${$array[$_]}[0] eq 'foobar'){ print ${$array[$_]}[1]; } # slower than... } print $hash{'foobar'}; #... this

2. Err, see above...

3. Define failure... I can't think of any circumstances where you could fail to do this - the reverse is more problematic, since you would have to be prepared to handle cases where two or more array values have the same 'key'.

I have to admit that I'm not really sure what you are asking - in general the type of data-structures that you are using will dictate whether a hash or an array is more suitable. If your keys are numeric, or if there isn't really a key, then use an array, but if the keys are anything else, then a hash would make sense.

map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
Tom Melly, pm@tomandlu.co.uk

In reply to Re: Data structure efficiency by Melly
in thread Data structure efficiency by shine22vn

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.