<>; @a=glob<>; @b=glob<>; $r[$_]=++$i for@a; for(@b){ if($c<$r[$_]){ push@o,$_; $c=$r[$_]; } } print join$/,sort{$a-$b}@o