I'm working on a script that asks for a 4 digit number.
When a number is then typed in, a search is made of an external text file to see if the number already exists.
The text file is in this format:
6393,Smith
2473,Jones
3769,Baker
6369,Evans
If the number exists, then that record only is to be displayed on screen and not written to the external file. If the record does not exist, then it is written to the external file.
My problem is my code displays all records and still writes the record to the external file.
Here is a cut down version of my code:
#!c:\perl\bin\perl.exe
print("Enter your four digit customer reference number.\n");
$ref=<STDIN>;
chomp($ref);
while ($ref!~m/^\d{4}$/)
{
print("Not a valid number, please enter your correct customer reference number.\n");
$ref=<STDIN>;
}
open(RECORDS,"Data.txt") || die "Couldn't open file:$!";
while(<RECORDS>)
{
if ($ref=~m/^\d{4}/) {
print"$_";
}
}
close(RECORDS);
open (RECORDS,">>Data.txt") || die "Couldn't open file:$!";
print RECORDS "$ref\n";
close(RECORDS);
Can anyone please help me?
Dave
When a number is then typed in, a search is made of an external text file to see if the number already exists.
The text file is in this format:
6393,Smith
2473,Jones
3769,Baker
6369,Evans
If the number exists, then that record only is to be displayed on screen and not written to the external file. If the record does not exist, then it is written to the external file.
My problem is my code displays all records and still writes the record to the external file.
Here is a cut down version of my code:
#!c:\perl\bin\perl.exe
print("Enter your four digit customer reference number.\n");
$ref=<STDIN>;
chomp($ref);
while ($ref!~m/^\d{4}$/)
{
print("Not a valid number, please enter your correct customer reference number.\n");
$ref=<STDIN>;
}
open(RECORDS,"Data.txt") || die "Couldn't open file:$!";
while(<RECORDS>)
{
if ($ref=~m/^\d{4}/) {
print"$_";
}
}
close(RECORDS);
open (RECORDS,">>Data.txt") || die "Couldn't open file:$!";
print RECORDS "$ref\n";
close(RECORDS);
Can anyone please help me?
Dave