Image

Imagecaladri wrote in Imagedailysrc

Ruby code to process friends data from LiveJournal URLs ala http://www.livejournal.com/misc/fdata.bml?user=flata and tell you who lists you that you don't list, and vice-versa.

#! /usr/bin/env ruby

friends = []
friendofs = []

def diff(array1, array1desc, array2, array2desc)
        array1.each do |array1entry|
                match = false
                array2.each do |array2entry|
                        if array1entry == array2entry then
                                match = true
                        end
                end
                if not match then
                        print "#{array1entry} is in #{array1desc} but not in #{array2desc}\n";
                end
        end
end

def diff2(array1, array1desc, array2, array2desc)
        diff(array1, array1desc, array2, array2desc)
        diff(array2, array2desc, array1, array1desc)
end

if not ARGV[0] then
        STDERR.print "usage: fdata.rb <fdata>\n"
        exit
end

IO.foreach(ARGV[0]) do |line|
        line.chomp!
        if line[0] == ?# then
                print "Processing friends data.\n"
                next
        elsif line[0] == nil then
                print "Done processing friends data.\n"
                diff2(friends, "your friends list",
                      friendofs, "your friend-ofs list")
                next
        end
        data = line.split(/ /)
        case data[0]
        when '<'        then friendofs
        when '>'        then friends
        else            print "Invalid input line: #{line}\n"
                        exit
        end.push(data[1])
end


For bonus points, look at the foldr using example below, and give an example of using foldr to create an array which can be used with .each to do the printing of the nand of the arrays.