#!/usr/bin/perl use strict; use warnings; my @strings; while () { # Split up input stream using dashes my @smallarray = split '-'; # @smallarray now has 'aaa bbb ccc', etc. # Iterate through @smallarray items and split them up using spaces # Create an array of anonymous array references # The references are the array resulting from split, using [] push(@strings, [split /\s/]) foreach (@smallarray); } #### # The array of anonymous arrays now looks like this: # # $VAR1 = [ # 'aaa', # 'bbb', # 'ccc' # ]; # $VAR2 = [ # 'ddd', # 'eee', # 'fff' # ]; # $VAR3 = [ # 'ggg', # 'hhh', # 'iii' # ]; # $VAR4 = [ # 'jjj', # 'kkk', # 'lll' # ]; #### # Print out the whole AoA contents foreach my $arrayref (@strings) { print "$_\n" foreach @$arrayref; } print "----\n\n"; # Or just the contents of one group foreach my $item (@{$strings[0]}) { print "$item\n"; } __DATA__ aaa bbb ccc-ddd eee fff-ggg hhh iii-jjj kkk lll- __OUTPUT__ aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk lll ---- aaa bbb ccc