http://www.perlmonks.org?node_id=93449


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 :
--
Brovnik