Last active
August 29, 2015 14:06
-
-
Save CliveIMPISA/0e8ee17a37846c925a93 to your computer and use it in GitHub Desktop.
YAML_to_TSV.rb
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
#!/usr/bin/env ruby | |
require 'yaml' | |
# Reading file from command line input and declaration of variables | |
stu_info = YAML.load(File.read(ARGV[0])) | |
flag = false | |
keyarr = [] | |
valuearr = [] | |
wholevalue = [] | |
# Method reads the key of only one hash element in the stu_info array. | |
# Every value of each hash in the array in copied to a temporary array | |
# That temp array is then pushed to an array of array called wholevalue | |
stu_info.each do |unique| | |
unique.each do |a, b| | |
keyarr.push a if flag == false | |
valuearr.push b | |
end | |
wholevalue.push valuearr | |
valuearr = [] | |
flag = true | |
end | |
# Function places the keyarr at the beginning of wholevalue array | |
wholevalue.unshift(keyarr) | |
# Data from wholevalue is written to a file | |
if ARGV[1] | |
File.open(ARGV[1], 'w') do |file| | |
wholevalue.each do |line| | |
file.puts line.join("\t") | |
end | |
end | |
else | |
File.open('Default.tsv', 'w') do |file| | |
wholevalue.each do |line| | |
file.puts line.join("\t") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment