This week, I showed them about split and asked them to write something to read the hash back in, and prompt for a name, then display that user's favourite colour. One of the students - a bright girl - said "Why should I use a hash?" and came up with something like this:
use strict;
open IN, "favourites.txt" or die $!;
print "Who do you want to know about? ";
my $input = <STDIN>;
chomp $input;
while (<IN>) {
chomp;
my ($name, $colour) = split/:/;
if ($input eq $name) {
print "$name's favourite colour is $colour\n";
}
}
What can you say? It's decent code, it fulfils the spec, it just wasn't exactly what I had imagined. Ah, the fun of TMTOWTDI...
But what everyone here wants to know is... (Score:3, Funny)
A bright girl ... in a Perl class ... is she single?
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
Reply to This
Re:But what everyone here wants to know is... (Score:2)
Re:But what everyone here wants to know is... (Score:2)
Retracted. [perl.org]
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
If it passes the tests... (Score:2, Interesting)
Now extend the problem. Ask for multiple names to match. Introduce Benchmark.pm.
Apologies if this is teaching you to suck eggs.
Re:If it passes the tests... (Score:3)
It got full marks, for doing the job and being clever than the solution I anticipated.
Now extend the problem. Ask for multiple names to match.
I did something similar: asked for it to report an error for names that weren't in the file. She quickly realised a hash-based solution was more appropriate for that.
Take off points... (Score:2, Funny)
:-) Don't hit me!