-
-
Save anonymous/580f37612e43abf2e9a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To run this script make sure it is in the same directory as cia-1966.xml (http://rubylearning.com/data/cia-1996.zip) | |
# and simply run the file via command line (ruby mattdahl.rb) | |
# Also note you need the hpricot gem so if you don't have it run (sudo gem install hpricot) | |
require 'rubygems' | |
gem 'hpricot' | |
require 'hpricot' | |
file = File.open('cia-1996.xml', 'r') | |
doc = Hpricot(file) | |
continents = (doc/'continent') | |
countries = (doc/'country') | |
puts '1.' | |
country = countries.sort_by {|country| country.attributes['population'].to_i}.last | |
puts "\t#{country.attributes['name']} - #{country.attributes['population']}" | |
puts '2.' | |
countries.sort_by {|country| country.attributes['inflation'].to_f}.reverse[0..4].each do |country| | |
puts "\t#{country.attributes['name']} - #{country.attributes['inflation']}" | |
end | |
puts '3.' | |
countries.group_by {|country| country.attributes['continent']}.sort.each do |continent, countries| | |
puts "\t#{continent}" | |
countries.sort_by {|country| country.attributes['name']}.each do |country| | |
puts "\t\t#{country.attributes['name']}" | |
end | |
end | |
file.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment