#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Order_disjoint_list_items#Sidef
#

func dsort(m, n) {
    var h = Hash()
    n.each {|item| h{item} := 0 ++ }
    m.map  {|item| h{item} := 0 -- > 0 ? n.shift : item}
}

<<'EOT'.lines.each { |line|
        the cat sat on the mat  | mat cat
        the cat sat on the mat  | cat mat
        A B C A B C A B C       | C A C A
        A B C A B D A B E       | E A D A
        A B                     | B
        A B                     | B A
        A B B A                 | B A
EOT
        var (a, b) = line.split('|').map{.words}...
        say "#{a.join(' ')} | #{b.join(' ')} -> #{dsort(a.clone, b.clone).join(' ')}"
}