in reply to Phone Number Storage
It would be better to store the data in a format which allows you to parse it
in later in the same program or in a future version.
--
Brovnik
As a start, use ',' as a separator, rather than ' ', otherwise it is difficult to know where the name ends and the email starts. E.G.
(Yes, you could write a whizzy regex to anchor the email, but a separator is easier).Frank Wright frank@lloyds.com 212-04567-98 Frank Lloyd Wright frank@wright.com 212-04567-98
print FILEHANDLENAME join(',',$name,$num,$email),"\n";
This would allow you later to search only the name, by splitting the input as in comes in. Otherwise you are also matching on the email address which may not be what you want. (In my eaxmple above, a search for 'lloyd' would have matched both lines.
Things to think about later :use strict; # This should go at the top of the file use warnings; # So should this, to catch errors. open FILE, "<storage.txt" or die $@; # report error on failure while (<FILE>) { my ($name,$email,$number) = split(/,/,$_); if ($name =~ /$name2/i) { print "$_\n"; } }
- What happens if the name added contains a ',' ? (Hint: consider using Text::CSV)
- How about adding command line access, e.g. easy -R Frank (Hint: consider Getopt::Std)
- If you are going to add more options later, (such as the ability to delete or change a name), how would you make the code maintainable ? (Hint: consider putting the actions each into their own subroutine)
--
Brovnik
|
---|
In Section
Cool Uses for Perl