This is not esoteric. Let me show some data structure access examples. In the below, imagine "key" being X and "value" being Y coordinates.

'C': #define X_coord 0 #define Y_coord 1 #define MAX_ROWS 3 /* graph is a 2-d array of ints each row has an X and a Y coordinate Normally graph would be built dynamically as an array of pointer to array. The same [x][y] syntax can be used like in a predefined array. The same [x][y] syntax works in Perl's array of references to arrays also. Although in most cases, I would consider this a "not good thing". */ for (int row =0; row <MAX_ROWS; row++) { int this_pointX = graph[row][X_coord]; int this_pointY = graph[row][Y_coord]; .... ) ########## Perl: using Array of Array, about the same as 'C' this UGLY... my $X_coord = 0; my $Y_coord = 1; foreach my $i (0..@graph-1) # forget 0..$#graph # needless use of special variable { my $this_pointX = $graph[$i][$X_coord]; my $this_pointY = $graph[$i][$Y_coord]; } ########## Perl: Better than above using Array of Array: foreach my $row (@graph) { my ($this_pointX, $this_pointY) = @$row; .... } ########## Perl: using Array of Hash: foreach my $point (@graph) # graph is Array of Hash # meaning an array of pointers # to hash tables { my $this_pointX = $point->{'X_coord'}; my $this_pointY = $point->{'Y_coord'}; .... } # there can be big advantages to avoiding this # position dependent [$index] stuff as well as the # need to pre-declare those indicies, The Perl AoH # works very well to represent some types of C 2-D arrays. # It is very easy to imagine how an implementer would # go in that direction.
Using Array of Hash for a C 2-D array is a very reasonable thing. The C code to implement a hash table is not huge, but it is non-trivial. If the underlying data could be better represented as a Hash of Array in Perl, then fine. It is very easy to imagine how an implementer would go in the direction of Array of Hash and miss the better Perl implementation of Hash of Array.

In reply to Re^7: the annoying keys "key" and "value" by Marshall
in thread the annoying keys "key" and "value" by jabowery

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.