my @add; for my $arr (@tmp_container) { if(@{$arr->[0]}>2) { my @val1=@{$arr->[0]}; my @val2=@{$arr->[1]}; @add=[\@val1,\@val2]; } else { unshift(@{$arr},@add); } }