Hi there, I'm very new to Perl so please excuse the newbie type questions...
First of all, this I made program used to store all inputted data into a text file but now it doesn't even get passed asking for the Customer Reference number. Any ideas why?
Secondly, when I input a Customer Ref number and there's already one existing in the text document it should display the data for the customer with that ID number. However this program displays ALL the data instead (and still proceeds to ask for other data when it shouldn't). Any suggestions?
Many thanks in advance. Here's the program:
#!c:\perl\bin\perl.exe
if (-e "CustData.txt")
{
$data_file="CustData.txt";
open(DAT, $data_file);
@raw_data=<DAT>;
close(DAT);
}
else
{
open(FILE,">CustData.txt");
}
print ("Please enter Customer Reference Number:\n");
$CustRef = <STDIN>;
chomp ($CustRef);
while ($CustRef!~m/^[0-9]{4}$/)
{
print ("Incorrect Customer Reference Number. Please try again:\n");
$CustRef = <STDIN>;
chomp ($CustRef);
}
foreach $customer (@raw_data)
{
chop($customer);
($i_d,$t_itle,$s_urname,$f_orename,$s_ex,$d_ob,$v_ision)=split(/\,/,$customer);
if ($i_d = $CustRef)
{
print "Title: $t_itle. Surname: $s_urname. Gender: $s_ex. Date of Birth: $d_ob. Vision Measurement: $v_ision.";
}
else
{
print ("Please enter new customer details.\n");
print ("Title:\n");
$TitleName = <STDIN>;
chomp ($TitleName);
$A = "Dr";
$B = "Lady";
$C = "Lord";
$D = "Miss";
$E = "Mr";
$F = "Mrs";
$G = "Ms";
$H = "Sir";
if ($TitleName ne $A)
{
if ($TitleName ne $B)
{
if ($TitleName ne $C)
{
if ($TitleName ne $D)
{
if ($TitleName ne $E)
{
if ($TitleName ne $F)
{
if ($TitleName ne $G)
{
if ($TitleName ne $H)
{
print ("Title not recognised. Please try again:\n");
$TitleName = <STDIN>;
chomp ($TitleName);
}
}
}
}
}
}
}
}
print ("Surname:\n");
$LastName = <STDIN>;
chomp ($LastName);
while ($LastName !~ /^[A-Za-z]{1,20}$/)
{
print ("Bad Name. Please re-enter:\n");
$LastName = <STDIN>;
chomp ($LastName);
}
print ("Forename:\n");
$FirstName = <STDIN>;
chomp ($FirstName);
while ($FirstName !~ /^[A-Za-z]{1,20}$/)
{
print ("Bad Name. Please re-enter:\n");
$FirstName = <STDIN>;
chomp ($FirstName);
}
print ("Gender:\n");
$Gender = <STDIN>;
chomp ($Gender);
$I = "M";
$J = "F";
if ($Gender ne $I)
{
if ($Gender ne $J)
{
print ("Gender not recognised. Please type M or F:\n");
$Gender = <STDIN>;
chomp ($Gender);
}
}
print ("Date of Birth:\n");
$Dob = <STDIN>;
chomp ($Dob);
while ($Dob !~ /^[0-9]{2}[A-Za-z'\-]{1}[0-9]{2}[A-Za-z'\-]{1}[0-9]{4}$/)
{
print ("Bad format. Please re-enter as DD-MM-YYYY:\n");
$Dob = <STDIN>;
chomp ($Dob);
}
print ("Please enter Vision Measurement:\n");
$Vm = <STDIN>;
chomp ($Vm);
while ($Vm !~ /^[0-9]{2}$/)
{
print ("Bad format. Please enter a number between 01 and 99:\n");
$Vm = <STDIN>;
chomp ($Vm);
}
print ("Thank you for completing Customer Details:\n");
if (-e "CustData.txt")
{
open(FILE,">>CustData.txt");
}
print FILE "$CustRef\,$TitleName\,$LastName\,$FirstName\,$Gender\,$Dob\,$Vm\n";
close(FILE);
}
}
First of all, this I made program used to store all inputted data into a text file but now it doesn't even get passed asking for the Customer Reference number. Any ideas why?
Secondly, when I input a Customer Ref number and there's already one existing in the text document it should display the data for the customer with that ID number. However this program displays ALL the data instead (and still proceeds to ask for other data when it shouldn't). Any suggestions?
Many thanks in advance. Here's the program:
#!c:\perl\bin\perl.exe
if (-e "CustData.txt")
{
$data_file="CustData.txt";
open(DAT, $data_file);
@raw_data=<DAT>;
close(DAT);
}
else
{
open(FILE,">CustData.txt");
}
print ("Please enter Customer Reference Number:\n");
$CustRef = <STDIN>;
chomp ($CustRef);
while ($CustRef!~m/^[0-9]{4}$/)
{
print ("Incorrect Customer Reference Number. Please try again:\n");
$CustRef = <STDIN>;
chomp ($CustRef);
}
foreach $customer (@raw_data)
{
chop($customer);
($i_d,$t_itle,$s_urname,$f_orename,$s_ex,$d_ob,$v_ision)=split(/\,/,$customer);
if ($i_d = $CustRef)
{
print "Title: $t_itle. Surname: $s_urname. Gender: $s_ex. Date of Birth: $d_ob. Vision Measurement: $v_ision.";
}
else
{
print ("Please enter new customer details.\n");
print ("Title:\n");
$TitleName = <STDIN>;
chomp ($TitleName);
$A = "Dr";
$B = "Lady";
$C = "Lord";
$D = "Miss";
$E = "Mr";
$F = "Mrs";
$G = "Ms";
$H = "Sir";
if ($TitleName ne $A)
{
if ($TitleName ne $B)
{
if ($TitleName ne $C)
{
if ($TitleName ne $D)
{
if ($TitleName ne $E)
{
if ($TitleName ne $F)
{
if ($TitleName ne $G)
{
if ($TitleName ne $H)
{
print ("Title not recognised. Please try again:\n");
$TitleName = <STDIN>;
chomp ($TitleName);
}
}
}
}
}
}
}
}
print ("Surname:\n");
$LastName = <STDIN>;
chomp ($LastName);
while ($LastName !~ /^[A-Za-z]{1,20}$/)
{
print ("Bad Name. Please re-enter:\n");
$LastName = <STDIN>;
chomp ($LastName);
}
print ("Forename:\n");
$FirstName = <STDIN>;
chomp ($FirstName);
while ($FirstName !~ /^[A-Za-z]{1,20}$/)
{
print ("Bad Name. Please re-enter:\n");
$FirstName = <STDIN>;
chomp ($FirstName);
}
print ("Gender:\n");
$Gender = <STDIN>;
chomp ($Gender);
$I = "M";
$J = "F";
if ($Gender ne $I)
{
if ($Gender ne $J)
{
print ("Gender not recognised. Please type M or F:\n");
$Gender = <STDIN>;
chomp ($Gender);
}
}
print ("Date of Birth:\n");
$Dob = <STDIN>;
chomp ($Dob);
while ($Dob !~ /^[0-9]{2}[A-Za-z'\-]{1}[0-9]{2}[A-Za-z'\-]{1}[0-9]{4}$/)
{
print ("Bad format. Please re-enter as DD-MM-YYYY:\n");
$Dob = <STDIN>;
chomp ($Dob);
}
print ("Please enter Vision Measurement:\n");
$Vm = <STDIN>;
chomp ($Vm);
while ($Vm !~ /^[0-9]{2}$/)
{
print ("Bad format. Please enter a number between 01 and 99:\n");
$Vm = <STDIN>;
chomp ($Vm);
}
print ("Thank you for completing Customer Details:\n");
if (-e "CustData.txt")
{
open(FILE,">>CustData.txt");
}
print FILE "$CustRef\,$TitleName\,$LastName\,$FirstName\,$Gender\,$Dob\,$Vm\n";
close(FILE);
}
}