Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Failing to Read/Search/Write to a Data collection programme

Status
Not open for further replies.

bhekis9

Programmer
Joined
Nov 3, 2005
Messages
1
Location
GB
I have following script but am failing to get correct result; I want to get record printed if it matches to data input and create new record if not found; Could any one help?


#!c:\perl\bin\perl.exe -W

open(RECORDS, "CustData.txt")|| DIE;

print("Enter your four digit Customer Reference Number: ");
$customerRef = <STDIN>;
chomp ($customerRef);

while ($customerRef<1000 || $customerRef>9999)
{
print("Incorrect Reference Number!
Please enter your correct Reference Number: ");
$customerRef = <STDIN>;
chomp ($customerRef);
}

$customerRef=<RECORDS>;
while(<RECORDS>)
{
print($customerRef);
exit 0;
}

open(RECORDS, ">>CustData.txt");

print("Enter your four digit Customer Reference Number: ");
$customerRef = <STDIN>;
chomp ($customerRef);

while ($customerRef<1000 || $customerRef>9999)
{
print("Incorrect Reference Number!
Please enter your correct Reference Number: ");
$customerRef = <STDIN>;
chomp ($customerRef);
}

@title = ("Dr", "Lady", "Lord", "Miss", "Mr", "Mrs", "Ms", "Sir");

print("Please enter your title: ");
$title = <STDIN>;
chomp ($title);

foreach (@title)

{
if ($title !~ /(Dr|Lady|Lord|Miss|Mr|Mrs|Ms|Sir)/)
{
print("Incorrect title! Choose from following list: ");
print("$_@title");
$title = <STDIN>;
chomp ($title);
}
}


print("Surname: ");
$surname = <STDIN>;
chomp ($surname);

while ($surname !~ /(^[A-Za-z]{1,20})$/)

{
print("Please enter your Surname in alphabetic characters, maximum of 20: ");
$surname = <STDIN>;
chomp ($surname);
}


print("Forename: ");
$forename = <STDIN>;
chomp ($forename);

if ($forename !~ /(^[A-Za-z]{1,20})$/)
{
print("Please enter your Forename in alphabetic characters, maximum of 20: ");
$forename = <STDIN>;
chomp ($forename);
}


print("Sex (M-Male or F-Female): ");
$sex = <STDIN>;
chomp ($sex);

while ($sex !~ /(M|F|m|f)/)
{
print("Please choose M for Male or F for female: ");
$sex = <STDIN>;
chomp ($sex);
}

print("Date of Birth (DD/MM/YYYY): ");
$dateOfBirth = <STDIN>;
chomp ($dateOfBirth);

if ($dateOfBirth !~ /(\d\d)\W(\d\d)\W(\d\d\d\d)/)
{
print("Please enter your birthday in this format DD/MM/YYYY: ");
$dateOfBirth = <STDIN>;
chomp ($dateOfBirth);
}


print("Vision Measurement: ");
$visionMeasurement = <STDIN>;
chomp ($visionMeasurement);

while ($visionMeasurement<1 || $visionMeasurement>99)
{
print("Please a number between 1 and 99: ");
$visionMeasurement = <STDIN>;
chomp ($visionMeasurement);
}

print(RECORDS "$customerRef,$title,$surname,$forename,$sex,$dateOfBirth,$visionMeasurement\n");

close(RECORDS);

Thanking you in advance!! Warm Regards
 
firstthing, is perl is case sensitive, and all it's functions are lower case (Ibelieve) so DIE will not work, it has to be die:

open(RECORDS, "CustData.txt") || die "$!";

you are getting the value of $customer from <STDIN> but immediately redefine it as the first line of the file you opened:

$customerRef=<RECORDS>;


and then you script exits in the subsequent while block:

Code:
while(<RECORDS>)
{
        print($customerRef);
        [b]exit 0;[/b]
}

that's as far as your script will go if. Maybe if you remove that exit command the script will continue and do what you want, but I didn't really look closely enough to tell if it will or not.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top