#!/usr/bin/env perl use strict; use warnings; use Data::Dump; my @a = qw{a b c e 1 2 3 4 5 3 f}; my $x = 3; dd [(map{@;==$x?do{$;=[@;];@;=($_);$;}:do{push(@;,$_);()}}@a),[@;]]; #### [["a", "b", "c"], ["e", 1, 2], [3, 4, 5], [3, "f"]]