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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using data stored in a text file

Status
Not open for further replies.

Rinoa666

Programmer
Mar 10, 2007
4
GB
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);

}
}
 
Sorry to bump the thread but any ideas, anyone?

My project's currently on hold down to this silly little thing! :(
 
The thing is, most everyone here knows this is a school/class assignment because we have seen this same script on more than a few occasions. And few people want to look over entire programs, especialy hard to read unformatted code, and debug a script. Narrow the problem down to a specific area of the script, use the code tags to post code:

[ignore]
Code:
you code here
[/ignore]

and maybe someone will give it a go. But really, posting school/class work is not allowed here.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
AFAIK, the shebang line notation (#!usr/bin perl) doesn't work on Windows.

Kev's right, we don't do people's homeworkwork for them, However, there is a difference between doing it for you, and pointing out errors. In this case it's a common gotcha for perl beginners, so I feel it's OK to help a little.

There is a big difference between assignment and comparison in perl. So in this instance
Code:
if ($i_d = $CustRef)
actually sets $i_d to the value of $CustRef, and returns that value of $i_d to the if statement. Perl will interpret just about anything other than zero as 1, and hence your 'comparison' is always true. I suspect what you probably want is
Code:
if ($i_d [red]==[/red] $CustRef)
which compares the two variables (numerically) and sets true or false accordingly.

There are many other things wrong with the code, design, etc. (most notably the way you validate the titles), but your tutor can help you with those. In the meantime, this will get you started.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
You also want to move the code that asks for new details OUTSIDE of the for() loop.

Use a flag variable to indicate that a match has been found. Set it when you have the match, and then after the for() loop check it before asking for new details.

 

Is this variable naming standard dictacted by your instructor?:

Code:
($i_d,$t_itle,$s_urname,$f_orename,$s_ex,$d_ob,$v_ision)=split(/\,/,$customer);

I find it difficult to read and wouldn't recommend it in the "real world"..... but maybe I'm wrong.
 
Steve & Brigmar - thank you for your help and suggestions, they were just what I needed to carry on :)

Nedmega - no, I don't have a tutor, I'm teaching myself Perl. My notes are VERY basic and I'm having to find online tutorials to pick up the rest. Thanks for pointing that out though, I'll look into it further, and hopefully I'll get there eventually :)

Kevin - thank you for the pointers, I'll be sure to use them next time I post. I must point out that I wasn't asking anyone to do my work for me, I just needed help figuring something out. I'm sorry I gave the impression that it was schoolwork I was doing but it's not. As I am just a beginner I'm doing exercises from my notes (probably taken from proper Perl classes? Who knows). Anyways, thanks for your time :)
 
Rinoa666,

I'm the first to apologize if I am wrong about something. So my apology if this was not school work for you. It is an assignment used in a class though as I have seen this same project/assignment/excersize on a few occasions on several forums and the posters said it was for their class work.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top