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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.