Skip to content

Instantly share code, notes, and snippets.

@dharshan
Created March 27, 2018 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dharshan/7e819801472d373899a7901492cf2c19 to your computer and use it in GitHub Desktop.
Save dharshan/7e819801472d373899a7901492cf2c19 to your computer and use it in GitHub Desktop.
Innova solutions count host number from file and write to other file
filename = gets.strip
res = []
File.open(filename).each do |line|
res << line.split(' ').first
end
res_hash = Hash.new(0)
res.each { |i| res_hash[i] += 1 }
text_array = res_hash.to_a.map { |res| res.join(' ') }
out_filename = 'records_'+filename
File.open(out_filename, 'w+') do |file|
text_array.each { |elem| file.puts(elem) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment