Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Phone Number Storage

by Brovnik (Hermit)
on Jul 03, 2001 at 14:11 UTC ( #93449=note: print w/ replies, xml ) Need Help??


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.

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.

Frank Wright frank@lloyds.com 212-04567-98 Frank Lloyd Wright frank@wright.com 212-04567-98
(Yes, you could write a whizzy regex to anchor the email, but a separator is easier).
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.

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"; } }
Things to think about later :
  • 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


Comment on Re: Phone Number Storage
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://93449]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2014-08-30 19:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls