Skip to content

Instantly share code, notes, and snippets.

@igrabes
Created March 8, 2012 15:00
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 igrabes/2001384 to your computer and use it in GitHub Desktop.
Save igrabes/2001384 to your computer and use it in GitHub Desktop.
Code Project
require 'date'
all_files = []
file_path = "./"
Dir.foreach(file_path) do |file_name|
next if file_name == "." || file_name == ".." || file_name == ".DS_Store" || file_name == "data_display.rb" || file_name == "cyrus_data_display" #potentially could refactor this into a while file_name.includes(/.rb/)
puts "got #{file_name}"
#Can refactor to a case statement
if file_name == "comma.txt"
File.open("comma.txt") do |line|
line.each do |l|
comma_new_line = l.chomp.split(', ')
reorder = comma_new_line.slice!(3)
comma_new_line << reorder
all_files << comma_new_line
end
end
elsif file_name == "pipe.txt"
File.open("pipe.txt") do |line|
line.each do |l|
pipe_new_line = l.gsub!(/-/, '/').chomp.split(' | ')
pipe_new_line.slice!(2)
reorder = pipe_new_line.slice!(3)
if pipe_new_line[2] == "M"
pipe_new_line[2].replace('Male')
end
pipe_new_line << reorder
all_files << pipe_new_line
end
end
elsif file_name == "space.txt"
File.open("space.txt") do |line|
line.each do |l|
space_new_line = l.gsub!(/-/, '/').chomp.split(' ')
space_new_line.slice!(2)
if space_new_line[2] == "F"
space_new_line[2].replace('Female')
end
all_files << space_new_line
end
end
end
end
first_output = all_files.sort { |a,b| [ a[2], a[0] ] <=> [ b[2], b[0] ] }
second_output = all_files.each do |x|
x[3] = Date.strptime(x[3], "%m/%d/%Y" )
end
second_output = all_files.sort { |a,b| a[3] <=> b[3] }
second_output.map do |d|
date_string = d[3].to_s.split("-")
year = date_string[0]
month = date_string[1]
day = date_string[2]
date_string = "#{month}/#{day}/#{year}"
d[3] = date_string
end
third_output = all_files.sort { |a,b| b[0] <=> a[0] }
File.new('cyrus_data_display', "w+")
puts "\n"
puts "This is the first output:"
first_output.each do |l|
puts "#{l[0]} #{l[1]} #{l[2]} #{l[3]} #{l[4]} \n"
end
puts "\n"
puts "This is the second output:"
second_output.each do |l|
puts "#{l[0]} #{l[1]} #{l[2]} #{l[3]} #{l[4]} \n"
end
puts "\n"
puts "This is the third output:"
third_output.map do |l|
puts "#{l[0]} #{l[1]} #{l[2]} #{l[3]} #{l[4]} \n"
end
# File.open('./cyrus_data_display', "w+") do |c|
# c.puts "This is the first output #{first_output} \n"
# c.puts "This is the second output #{second_output} \n"
# c.puts "This is the third output #{third_output}\n"
# end
# Steps:
# 1. Had to load all of the files
# 2. Loop through files
# 3. Condtionally opened the files based on what type of delimeter they had
# 4. Once the files were open I needed to loop through each line
# 5. Once I had each line I needed to remove their respective delimeters and converting to an array
# 6. I then formatted the date properly by substituting out the '-' with '/'
# 7. Had to reorder the date and favorite color in the comma and pipe files
# 8. Realized that there were extra data points in the pipe and space files
# 9. Had to remove the extra data point
#10. Had to replace "F" or "M" with "Female" and "Male" respectively
#11. Need to set up sort functions according to the instructions
require 'date'
all_files = []
file_path = "/Users/iangrabill/Desktop/programs/cyrus_code_test"
Dir.foreach(file_path) do |file_name|
next if file_name == "." || file_name == ".." || file_name == ".DS_Store" || file_name == "data_display.rb" #potentially could refactor this into a while file_name.includes(/.rb/)
puts "got #{file_name}"
if file_name == "comma.txt"
File.open("comma.txt") do |line|
line.each do |l|
comma_new_line = l.chomp.split(', ')
reorder = comma_new_line.slice!(3)
comma_new_line << reorder
# puts comma_new_line.inspect
all_files << comma_new_line
end
end
elsif file_name == "pipe.txt"
File.open("pipe.txt") do |line|
line.each do |l|
pipe_new_line = l.gsub!(/-/, '/').chomp.split(' | ')
pipe_new_line.slice!(2)
reorder = pipe_new_line.slice!(3)
if pipe_new_line[2] == "M"
pipe_new_line[2].replace('Male')
end
pipe_new_line << reorder
all_files << pipe_new_line
end
end
elsif file_name == "space.txt"
File.open("space.txt") do |line|
line.each do |l|
space_new_line = l.gsub!(/-/, '/').chomp.split(' ')
space_new_line.slice!(2)
if space_new_line[2] == "F"
space_new_line[2].replace('Female')
end
all_files << space_new_line
end
end
end
end
first_output = all_files.sort { |a,b| [ a[2], a[0] ] <=> [ b[2], b[0] ] }
puts "This is the first output #{first_output}"
# second_output = all_files.each do |x|
# date = Date.parse x[3]
# puts date
# end
#
# second_output = all_files.sort { |a,b| b[3] <=> a[3] }
# puts "This is the second output #{second_output}"
third_output = all_files.sort { |a,b| b[0] <=> a[0] }
puts "This is the third output #{third_output}"
# Steps:
# 1. Had to load all of the files
# 2. Loop through files
# 3. Condtionally opened the files based on what type of delimeter they had
# 4. Once the files were open I needed to loop through each line
# 5. Once I had each line I needed to remove their respective delimeters and converting to an array
# 6. I then formatted the date properly by substituting out the '-' with '/'
# 7. Had to reorder the date and favorite color in the comma and pipe files
# 8. Realized that there were extra data points in the pipe and space files
# 9. Had to remove the extra data point
#10. Had to replace "F" or "M" with "Female" and "Male" respectively
#11. Need to set up sort functions according to the instructions
require 'date'
all_files = []
file_path = "./"
Dir.foreach(file_path) do |file_name|
next if file_name.length < 3 || file_name.split(".").last == "rb"
puts "got #{file_name}"
if file_name == "comma.txt"
File.open(file_name) do |line|
line.each do |l|
comma_new_line = l.chomp.split(', ')
reorder = comma_new_line.slice!(3)
comma_new_line << reorder
all_files << comma_new_line
end
end
elsif file_name == "pipe.txt"
File.open(file_name) do |line|
line.each do |l|
pipe_new_line = l.gsub!(/-/, '/').chomp.split(' | ')
pipe_new_line.slice!(2)
reorder = pipe_new_line.slice!(3)
pipe_new_line[2].replace('Male') if pipe_new_line[2] == "M"
pipe_new_line << reorder
all_files << pipe_new_line
end
end
elsif file_name == "space.txt"
File.open(file_name) do |line|
line.each do |l|
space_new_line = l.gsub!(/-/, '/').chomp.split(' ')
space_new_line.slice!(2)
space_new_line[2].replace('Female') if space_new_line[2] == "F"
all_files << space_new_line
end
end
end
end
first_output = all_files.sort { |a,b| [ a[2], a[0] ] <=> [ b[2], b[0] ] }
second_output = all_files.each do |x|
x[3] = Date.strptime(x[3], "%m/%d/%Y" )
end
second_output = all_files.sort { |a,b| a[3] <=> b[3] }
second_output.map do |d|
date_string = d[3].to_s.split("-")
year = date_string[0]
month = date_string[1]
day = date_string[2]
date_string = "#{month}/#{day}/#{year}"
d[3] = date_string
end
third_output = all_files.sort { |a,b| b[0] <=> a[0] }
puts "\n"
puts "Output 1:"
first_output.each do |l|
puts "#{l[0]} #{l[1]} #{l[2]} #{l[3]} #{l[4]} \n"
end
puts "\n"
puts "Output 2:"
second_output.each do |l|
puts "#{l[0]} #{l[1]} #{l[2]} #{l[3]} #{l[4]} \n"
end
puts "\n"
puts "Output 3:"
third_output.each do |l|
puts "#{l[0]} #{l[1]} #{l[2]} #{l[3]} #{l[4]} \n"
end
# Steps:
# 1. Had to load all of the files
# 2. Loop through files
# 3. Condtionally opened the files based on what type of delimeter they had
# 4. Once the files were open I needed to loop through each line
# 5. Once I had each line I needed to remove their respective delimeters and converting to an array
# 6. I then formatted the date properly by substituting out the '-' with '/'
# 7. Had to reorder the date and favorite color in the comma and pipe files
# 8. Realized that there were extra data points in the pipe and space files
# 9. Had to remove the extra data point
#10. Had to replace "F" or "M" with "Female" and "Male" respectively
#11. Need to set up sort functions according to the instructions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment