Personally, I'd do this using an AOH. That keeps the data separate from the index, allowing the data to change format later without having to change other code (yes, you can use the -1 index, but all you have to do is use '3' as your index once to get confounded by later changes), and also allowing easy access to the original data without having to play with it:

#!/usr/bin/perl -w use strict; my $arr = ['A -4 C','C -4 B','B -4 A','A -2 C','C -3 B']; my @a = do { my $idx = 0; map { { data => [ split ' ', $_ ], idx => $idx++, } }@$arr }; use Data::Dumper; print Dumper(\@a);
This gives:
$VAR1 = [ { 'idx' => 0, 'data' => [ 'A', '-4', 'C' ] }, { 'idx' => 1, 'data' => [ 'C', '-4', 'B' ] }, { 'idx' => 2, 'data' => [ 'B', '-4', 'A' ] }, { 'idx' => 3, 'data' => [ 'A', '-2', 'C' ] }, { 'idx' => 4, 'data' => [ 'C', '-3', 'B' ] } ];
If you really want it in a single AoA, then change the map like this:
my @a = do { my $idx = 0; map { [ (split ' ', $_), $idx++ ], }@$arr };
HTH


In reply to Re^3: Using Map to Create AoA by Tanktalus
in thread Using Map to Create AoA by neversaint

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.