in reply to creating arrays of arrays
push will also work without explicitly defining your sub-arrays and getting references to them, as in the following:
use strict; use warnings; use Data::Dumper; my @letter_array = (); my @letters = ('A' .. 'Z'); foreach my $letter (@letters) { my $selection = int rand 4; push @{$letter_array[$selection]}, $letter; } print Dumper(\@letter_array);
Will produce (in at least one case):
$VAR1 = [ [ 'F', 'K', 'O', 'T', 'U', 'V', 'X' ], [ 'B', 'I', 'Q', 'W' ], [ 'A', 'D', 'E', 'H', 'J', 'L', 'M', 'P', 'S', 'Z' ], [ 'C', 'G', 'N', 'R', 'Y' ] ];
This works because of autovivification. Check perlref in your docs if this fifty-pfenning term piques your interest.
|
|---|