Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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 212-04567-98 Frank Lloyd Wright 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)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://93449]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2018-06-19 05:21 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.