Created
September 27, 2013 03:53
-
-
Save davidbella/6723886 to your computer and use it in GitHub Desktop.
Ruby: Examples of splitting
This file contains hidden or 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
# Given this List of Songs, Construct Arrays by Artist and Album | |
# Hint: Make use of the split() String Method | |
# http://www.ruby-doc.org/core-1.9.3/String.html#method-i-split | |
# Simple Example of Data Parsing | |
songs = [ | |
"The Magnetic Fields - 69 Love Songs - Parades Go By", | |
"The Magnetic Fields - Get Lost - Smoke and Mirrors", | |
"Neutral Milk Hotel - In An Aeroplane Over the Sea - Holland 1945", | |
"The Magnetic Fields - Get Lost - You, Me, and the Moon", | |
"The Magnetic Fields - 69 Love Songs - The Book of Love", | |
"Neutral Milk Hotel - In An Aeroplane Over the Sea - The King of Carrot Flowers" | |
] | |
# Your goal is to get it to print this list: | |
# Neutral Milk Hotel - In An Aeroplane Over the Sea - Holland 1945 | |
# Neutral Milk Hotel - In An Aeroplane Over the Sea - The King of Carrot Flowers | |
# The Magnetic Fields - 69 Love Songs - Parades Go By | |
# The Magnetic Fields - 69 Love Songs - The Book of Love | |
# The Magnetic Fields - Get Lost - Smoke and Mirrors | |
# The Magnetic Fields - Get Lost - You, Me, and the Moon | |
# puts songs.sort | |
neutral_milk_hotel = [] | |
the_magnetic_fields = [] | |
songs.each do |song| | |
if song.split(' - ').first == "Neutral Milk Hotel" | |
neutral_milk_hotel << song | |
else | |
the_magnetic_fields << song | |
end | |
end | |
in_an = [] | |
love_songs = [] | |
get_lost = [] | |
neutral_milk_hotel.each do |song| | |
if song.split(' - ')[1] == "In An Aeroplane Over the Sea" | |
in_an << song | |
end | |
end | |
the_magnetic_fields.each do |song| | |
if song.split(' - ')[1] == "69 Love Songs" | |
love_songs << song | |
else | |
get_lost << song | |
end | |
end | |
puts in_an | |
puts love_songs | |
puts get_lost |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment