Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.lisp
Re: first Lisp program: not sure what to do
William James
1/7/2016 1:20:00 AM
christaylor wrote:
> I'm basically a clueless newbie that's just started playing around
> with Common Lisp, and I've decided on the first small project to learn
> some things. I've got some basic things working, but I just can't get
> a big-picture idea of the best way to approach what I want to do.
> Sorry if this is really simple- here's an example of what I'm trying
> to do, using a contrived list of movies and the stars, genre, etc.
>
> Say I have a series of text files that look something like this:
>
> BLADE RUNNER
> Main Star: Harrison Ford
> Setting: Future
> Genre: Science Fiction
>
> CABARET
> Main Star: Liza Minelli
> Setting: 20th Century
> Genre: Musical
>
> ALL THAT JAZZ
> Main Star: Roy Scheider
> Setting: 20th Century
> Genre: Musical
>
> I have another series of text files that look like this:
>
> BLADE RUNNER
> Director: Ridley Scott
>
> CABARET
> Director: Bob Fosse
>
> ALL THAT JAZZ
> Director: Bob Fosse
>
> I want to read the files in and do something with them, and then print
> out something like:
>
> BLADE RUNNER
> Dir: Ridley Scott
> Main Star: Harrison Ford
> Setting: Future
> Genre: Science Fiction
>
> CABARET
> Dir: Bob Fosse
> Main Star: Liza Minelli
> Setting: 20th Century
> Genre: Musical
> See also: ALL THAT JAZZ
>
> ALL THAT JAZZ
> Dir: Bob Fosse
> Main Star: Roy Scheider
> Setting: 20th Century
> Genre: Musical
> See also: CABARET
>
> So basically, I'm reading in two different series of text files,
> finding the similarities, and printing out a summary that consolidates
> the information in little easier-to-read format. Reading the files in
> and printing them out is no problem, I have that working fine. I want
> to show things like places where the same star or director show up
> multiple times. I have some Lisp code that makes a hash table where I
==== file1.txt ====
Santa Fe Trail
star: Errol Flynn
setting: 19th century
genre: drama
Braveheart
star: Mel Gibson
setting: middle ages
genre: drama
Thanks for Everything
star: Jack Haley
setting: 20th century
genre: comedy
Conspiracy Theory
star: Mel Gibson
setting: 20th century
genre: drama
The Adventures of Robin Hood
star: Errol Flynn
genre: adventure
Wake Up and Live
star: Jack Haley
genre: comedy
setting: 20th century
Allegheny Uprising
star: John Wayne
setting: 18th century
genre: drama
==== file2.txt ====
Thanks for Everything
director: William A. Seiter
Allegheny Uprising
director: William A. Seiter
Santa Fe Trail
director: Michael Curtiz
Conspiracy Theory
director: Richard Donner
Braveheart
director: Mel Gibson
The Adventures of Robin Hood
director: Michael Curtiz
Wake Up and Live
director: Sidney Lanfield
========
MatzLisp (Ruby):
## The hash-tables.
$stars = Hash.new{|hash,key| hash[key] = []}
$directors = Hash.new{|hash,key| hash[key] = []}
$movies = Hash.new {|hash,key| hash[key] = {}}
$fields = %w(director star setting genre)
["file1.txt","file2.txt"].each{|filename|
IO.read(filename).strip.split(/\s*\n\s*\n\s*/).each{|movie|
lines = movie.lines.map(&:strip)
title = lines.shift
lines.each{|line|
key, val = line.split(/:\s*/, 2)
$movies[title][key] = val
if "star" == key
$stars[val] << title
end
if "director" == key
$directors[val] << title
end } } }
$movies.each{|title, data|
puts title
$fields.each{|fieldname|
val = data[fieldname]
next unless val
puts "#{fieldname}: #{val}"
}
other_titles =
($stars[data["star"]] + $directors[data["director"]]).uniq - [title]
unless other_titles.empty?
puts "See also: #{other_titles.join '; '}"
end
puts }
==== output ====
Santa Fe Trail
director: Michael Curtiz
star: Errol Flynn
setting: 19th century
genre: drama
See also: The Adventures of Robin Hood
Braveheart
director: Mel Gibson
star: Mel Gibson
setting: middle ages
genre: drama
See also: Conspiracy Theory
Thanks for Everything
director: William A. Seiter
star: Jack Haley
setting: 20th century
genre: comedy
See also: Wake Up and Live; Allegheny Uprising
Conspiracy Theory
director: Richard Donner
star: Mel Gibson
setting: 20th century
genre: drama
See also: Braveheart
The Adventures of Robin Hood
director: Michael Curtiz
star: Errol Flynn
genre: adventure
See also: Santa Fe Trail
Wake Up and Live
director: Sidney Lanfield
star: Jack Haley
setting: 20th century
genre: comedy
See also: Thanks for Everything
Allegheny Uprising
director: William A. Seiter
star: John Wayne
setting: 18th century
genre: drama
See also: Thanks for Everything
--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: first Lisp program: not sure what to do
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password