2

So I'm given a text file containing:

Blade Runner (1982) [117 min] 
Full Metal Jacket (1987) [116 min] 
Monty Python and the Holy Grail (1975) [91 min] 
The Godfather (1972) [175 min]

and have to turn it into this:

Movie name: Blade Runner  
Movie release year: 1982 
Movie length (in mins): 117 

Movie name: Full Metal Jacket  
Movie release year: 1987 
Movie length (in mins): 116 

Movie name: Monty Python and the Holy Grail  
Movie release year: 1975 
Movie length (in mins): 91 

Movie name: The Godfather  
Movie release year: 1972 
Movie length (in mins): 175

First I iterate on each line, then I thought I should iterate on each part of the string, but that's where I get stuck, how do I do that? Do I use regex? How do I save a specific string that matches a regex?

Here's the current shell of the code, it stores the three parts into variables which are used to initialize a movie class who's to_s method prints in the desired format.

I know it's not right in many ways, but that's why I'm asking for help. variable = /regex/ is the line where the variable is assigned the thing captured by the regex and when /regex/ is for when that regex is matched.

class Movie
    def initialize (name, year, length) # constructor
        @name = name
        @year = year
        @length = length
    end

    def to_s    # returns string representation of object
        return "Movie Name: " + @name
            + "\nMovie release year: "
            + @year + "\nMovie Length (in min): "
            + @length + "\n"
    end
end

$movies = []
File.open("movies.txt").each do |line|
  if matches = /(.*)? \((\d+).*?(\d+)/.match(line)
    $movies << Movie.new(matches[1], matches[2], matches[3])
  end
end


for $movie in $movies do #what u got here is not index but the element in the array
    print $movie.to_s
end

Edit:

Fixed version of code, but print loop at end doesn't work.

Edit2: and nownit does. Thanks PeterPeiGuo!

1
  • @赢郭88888888: Can you explain your edit? Seems a bit much to go and destroy two-thirds of a question's content, and to top it off not even write a proper edit summary. Commented Feb 28, 2012 at 14:56

3 Answers 3

3
m = /(.*)? \((\d+).*?(\d+)/.match("Blade Runner (1982) [117 min]")
Sign up to request clarification or add additional context in comments.

6 Comments

Then what? That wasn't all I was asking about.
You edited it to make my comment dumb. :P But thank you. Why does m become an array with exactly those elements? (yes I would like there to be no whitespace before or after the strings stored, handled with the regex, not chomp/trim)
Awesome. So the regex looks for the whole expression basically, but only stored the things that match the inside of the curved brackets?
Problem. :( For some reason the loop at the end fails for printing the objects. I get: "Cannot convert Movie into Integer"
I modified last couple of lines and commented on the change
|
2

You can do something like this:

$movies = []
File.open("movies.txt").each do |line|
  if matches = /^(.*)\((\d+)\) \[(\d+)\smin\]/.match(line)
    $movies << Movie.new(matches[1], matches[2], matches[3])
  end
end

1 Comment

What? Ok, I did tag this as homework, I'm gonna need some explaining please. :P I'm ok on regex from JavaScript, but what are you doing here?
2
# create string containing list of movies (numerous ways to load this data)
movie = <<-MOV
Blade Runner (1982) [117 min] 
Full Metal Jacket (1987) [116 min] 
Monty Python and the Holy Grail (1975) [91 min] 
The Godfather (1972) [175 min]
<<-MOV

# split movies into lines, then iterate over each line and do some regex
# to extract relavent data (name, year, runtime)
data = movies.split("\n").map do |s| 
  s.scan(/([\w\s]+)\ \((\d+)\)\ \[(\d+)\ min\]/).flatten }
end
# => [['Blade Runner', '1982', '117'], ... ]

# iterate over data, output in desired format.
data.each do |data| 
  puts "Movie name: #{data[0]}\nMovie release year: #{data[1]}\nMovie length: (in mins): #{data[2]}\n\n" }
end
# outputs in format you specified

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.