@list = sort({ $a <=> $b } split(/ /, scalar(<>))); while(<>){ print; if($. - 1 == $list[0]){ print; shift@list; } }