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:
This gives:#!/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);
$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' ] } ];
HTHmy @a = do { my $idx = 0; map { [ (split ' ', $_), $idx++ ], }@$arr };
In reply to Re^3: Using Map to Create AoA
by Tanktalus
in thread Using Map to Create AoA
by neversaint
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |