On Sun, Dec 11, 2011 at 12:57:27PM +0100, kurtz le pirate wrote:
> Hello,

Hello:

> I want to have a two dim array. I build it like this:
>   
>   my @Cylinders;
> 
> 
>   /.*(<.*>),(<.*>).*/;
>   push @{$Cylinders[0]}, $1;
>   push @{$Cylinders[1]}, $2;
> 
> 
> Seems to work but a get an array with '2' lines and 'n' columns  
> instead of 'n' lines with '2' columns'.

Perl doesn't actually have multidimensional arrays. Arrays and
hashes can only store scalar values: strings, numbers, and
references (and undef). You can autovivify, as you were, or
explicitly store array references in an array (the same can be
done with a hash, or with hash references).

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my @multi_array;

$multi_array[0][0] = '[0][0]';
$multi_array[0][1] = '[0][1]';

push @{$multi_array[1]}, '[1][0]';
push @{$multi_array[1]}, '[1][1]';
push @{$multi_array[2]}, '[2][0]';
push @{$multi_array[2]}, '[2][1]';

$multi_array[3] = ['[3][0]', '[3][1]'];

print Dumper \@multi_array;

__END__

Output:

$VAR1 = [
          [
            '[0][0]',
            '[0][1]'
          ],
          [
            '[1][0]',
            '[1][1]'
          ],
          [
            '[2][0]',
            '[2][1]'
          ],
          [
            '[3][0]',
            '[3][1]'
          ]
        ];

You can learn about Perl data structures in `perldoc perldsc' and
arrays of arrays in `perldoc perllol'. See `perldoc perl' for a
list of other manual pages that might interest you.

Regards,


-- 
Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bamccaig.com/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'

Attachment: signature.asc
Description: Digital signature

Reply via email to