use Algorithm::Loops 'MapCar'; my @results = map { grep(/\n/,@$_) ? MapCar {[@_]} map([split "\n"], @$_) : $_ } @AoA;