Ted Flethuseo <flethuseo / gmail.com> wrote:
> [ merging two files ]
>
> I need to add to this file the corresponding ranks, preferably without
> scanning the file more than once.
> 
> So I grab the file of pairs and make it an array like this:
> [...]

first file
> 
> pair -- rank
> 4,2 -- 1
> 4,5 -- 2
> 6,8 -- 3
> 8,2 -- 4
> 

Use a hash:

pairranks = {}
File.foreach("pairs.txt") do |line|
  k, v = line.split(/ *-- */)
  pairranks[k] = v
end

second file:
> 
> pair
> 8,2
> 6,8
> 4,5
> 4,2
> 
File.open("result.txt", 'w') do |f|
  File.foreach("sorted.txt") do |line|
    line.chomp!
    f << line << pairranks[line]
  end
end

I leave error handling (missing values etc) to the reader.

Klaus

PS: this is untested code! may contain syntax and other errors.
-- 
http://lapiz.istik.de/

The Answer is 42. And I am the Answer. Now I am looking for the Question.