#!/usr/bin/perl use strict; use warnings; use Data::Dumper; print Dumper P(\@ARGV); sub P { my($m,$e,$p)=(@_,[],[]); !@{$m}and push@{$p},$e or P(@_=@{$m}and pop and[@_],[@{$e}],$p)and P(@_=@{$m}and pop and[@_],[sort@{$e},$m->[-1]],$p); [sort{@{$a}<=>@{$b}}@{$p}] }