in reply to Separating array contents

This creates an array of arrays split at "!", potentially with empty entries at beginning or end. Similar to choroba's complex solution in Re^2: Separating array contents

use strict; use warnings; use Data::Dumper; my @arr = qw/ ! my name ! is ! Achint ! I need ! help ! /; my @out = ( [] ); for( @arr ) { if( /^!$/ ) { push @out, []; } else { push @{ $out[-1] }, $_; } } print Dumper \@out;

Replies are listed 'Best First'.
Re^2: Separating array contents
by space_monk (Chaplain) on Jun 07, 2013 at 12:29 UTC

    If you don't like empty entries you can simply push the result array through a map or grep filter.

    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)