use List::Util qw[ reduce ]; sub p09 { @_ ? reduce{ $a->[-1][0] eq $b ? push @{ $a->[ -1 ] }, $b : push @{ $a }, [ $b ]; $a; } [[shift]], @_ :() }