If I have an input as below:
a b 703
o z 403
l p 550
d m 650
How can I make it to work so that the list sorts by the second column that is. The above list should print
a b 703
d m 650
l p 550
o z 403
Below is my code, what I thought was I could iterate through the first column and then sort the second column by key but it doesn't seem to be working.
hash = {}
lines = file.readlines
lines.each{|line|
if line =~ /^([A-Za-z]+) ([A-Za-z]+) (\d+)$/
hash[$1.to_s] = {} if hash[$1.to_s] == nil
hash[$1.to_s][$2.to_s] = $3.to_i
end
}
hash.each{|k,v|
array = Hash[v.sort]
array.each{|x,y|
puts "#{k} #{x} #{y}"
}
}
The code prints as the original one. I really want to do this using hashes as I am learning hashes.