Mad Libs: Difference between revisions
Content added Content deleted
(Mad Libs en BASIC256) |
(→{{header|rust}}: Rust version) |
||
Line 2,440: | Line 2,440: | ||
Replace:<noun> with:?cat |
Replace:<noun> with:?cat |
||
Fred went for a walk in the park. he found a cat. Fred decided to take it home. |
Fred went for a walk in the park. he found a cat. Fred decided to take it home. |
||
</pre> |
|||
=={{header|Rust}}== |
|||
{{libheader|regex}} |
|||
<lang rust>extern crate regex; |
|||
use regex::Regex; |
|||
use std::collections::HashMap; |
|||
use std::io; |
|||
fn main() { |
|||
let mut input_line = String::new(); |
|||
let mut template = String::new(); |
|||
println!("Please enter a multi-line story template with <parts> to replace, terminated by a blank line.\n"); |
|||
loop { |
|||
io::stdin() |
|||
.read_line(&mut input_line) |
|||
.ok() |
|||
.expect("The read line failed."); |
|||
if input_line.trim() == "" { |
|||
break; |
|||
} |
|||
template.push_str(&input_line); |
|||
input_line.clear(); |
|||
} |
|||
let re = Regex::new(r"<[^>]+>").unwrap(); |
|||
let mut parts: HashMap<_, _> = re |
|||
.captures_iter(&template) |
|||
.map(|x| (x.get(0).unwrap().as_str().to_string(), "".to_string())) |
|||
.collect(); |
|||
if parts.len() == 0 { |
|||
println!("No <parts> to replace.\n"); |
|||
} else { |
|||
for (k, v) in parts.iter_mut() { |
|||
println!("Please provide a replacement for {}: ", k); |
|||
io::stdin() |
|||
.read_line(&mut input_line) |
|||
.ok() |
|||
.expect("The read line failed."); |
|||
*v = input_line.trim().to_string(); |
|||
println!(""); |
|||
template = template.replace(k, v); |
|||
input_line.clear(); |
|||
} |
|||
} |
|||
println!("Resulting story:\n\n{}", template); |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Please enter a multi-line story template with <parts> to replace, terminated by a blank line. |
|||
<name> went for a walk in the park. <he or she> |
|||
found a <noun>. <name> decided to take it home. |
|||
Please provide a replacement for <name>: |
|||
John Wick |
|||
Please provide a replacement for <he or she>: |
|||
He |
|||
Please provide a replacement for <noun>: |
|||
puppy |
|||
Resulting story: |
|||
John Wick went for a walk in the park. He |
|||
found a puppy. John Wick decided to take it home. |
|||
</pre> |
</pre> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |