in reply to Birthday List
It makes your intent clearer. (That it's also more efficient is a bonus.)$choice = lc $choice; # ... later ... if ($choice eq 'l')
Then, I realized that there is a perfectly valid reason to do it your way - if the file is really large. You chose the memory-light method. I chose the coding-light method. *shrugs* TMTOWDI.my $filename = 'birthday.db'; # ... later ... open IN, $filename || die "Cannot read '$filename': $!\n"; print <IN>; close IN;
use constant CHOICE_LIST => 'l'; my $db_file = 'birthday.db'; # ... later ... if ($choice eq CHOICE_LIST) { open(IN, $db_file) || die "Cannot read '$db_file': $!\n"; print <IN>; close IN; }
This way, you re-use as many lines of code as possible.if ($choice eq CHOICE_MODIFY) { # Use a hash, not an array. The assumption here is that the names +will be unique. open(IN, $db_file) || die "Cannot read '$db_file': $!\n"; my %birthdays = map { chomp; split ':' } <IN>; close IN; # Print out a list of the names and birthdays print "$_ => $birthdays{$_}\n" for sort keys %birthdays; print "Enter record to modify: "; chomp ($key = <STDIN>); print "(M)odify or (D)elete? "; chomp ($choice = <STDIN>); $choice = lc $choice; if ($choice eq 'm') { print "Enter person's birthday: "; chomp ($date = <STDIN>); $birthdays{$key} = $date; } elsif ($choice eq 'd') { delete $birthdays{$key}; } open OUT, $db_file || die "Cannot overwrite '$db_file': $!\n"; print OUT "$_:$birthdays{$_}\n" for keys %birthdays; close OUT; }
------
We are the carpenters and bricklayers of the Information Age.
The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Birthday List
by Drgan (Beadle) on Aug 29, 2003 at 00:06 UTC | |
by dragonchild (Archbishop) on Sep 01, 2003 at 19:19 UTC |