Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Simon (89)

AOL IM: lathosjp (Add Buddy, Send Message)

Busy Man.

Journal of Simon (89)

Tuesday June 18, 2002
08:10 AM

People are ingenious

[ #5734 ]
On the Perl course I'm doing, I give out a lot of programming assignments. Last week, people wrote something to get five names and favourite colours from the user, stored each one into a hash, and then wrote out the hash as a colon-delimited file.

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>) {
    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...

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.