| |||||||||||
| |||||||||||
adding comas and newline into write out data Posted by womblebat (womblebat), 28 June 2007 hi,hope someone can help me here, i have created a form for user input to be written out into a CustData.txt file this is working fine but every entry just adds onto the same line in the .txt file, what i'm after is for every entry to be seperated by a comma and then a seperate line for each seperate user eg 1234,Mr,Greener,Michael,M,17/04/2000,43 1523,Ms,Hetfield,Stacey,F,06/11/1994,67 also if a reference number is entered it needs to be checked in the .txt file for existence and if so the details displayed hope this is clear for everyone ![]() here is the code i have so far #!c:\perl\bin\perl.exe open RECORDS, "CustData.txt" or die $!; open FILE, ">>CustData.txt" or die $!; # Customer Reference Input print ("Please Enter Your Four Digit Reference Number: "); $CustRef = <STDIN>; chomp ($CustRef); while ($CustRef !~ m/^[0-9]{4}$/) { print ("Incorrect Please Try Again:\n"); $CustRef = <STDIN>; chomp ($CustRef); } # Customer Title Input @Title = ("Dr","Lady","Lord","Miss","Mr","Mrs","Ms","Sir"); print ("Please Enter Your Title: "); $Title = <STDIN>; chomp ($Title); foreach (@Title) { if ($Title !~/(Dr|dr|Lady|lady|Lord|lord|Miss|miss|Mr|mr|Mrs|mrs|Ms|ms|Sir|sir)/) { print("Incorrect Title! Please choose from the following list:\n"); print("$_@Title\n"); $Title = <STDIN>; chomp ($Title); } } # Customer Surname Input print ("Surname: "); $Surname = <STDIN>; chomp ($Surname); while ($Surname !~/(^[A-Za-z]{1,20})$/) { print ("Please enter your surname in alphabetic characters,max of 20 characters please: "); $Surname = <STDIN>; chomp ($Surname); } # Customer forename input print ("Forename: "); $Forename = <STDIN>; chomp ($Forename); if ($Forename !~/(^[A-Za-z]{1,20})$/) { print ("Please enter your forename in alphabetic characters,maximum of 20 characters please: "); $Forename = <STDIN>; chomp ($Forename); } # Customer gender input print ("Sex (M-male or F-female): "); $Sex = <STDIN>; chomp ($Sex); while ($Sex !~/(M|F|m|f)/) { print ("Please enter M for Male or F for Female: "); $Sex = <STDIN>; chomp ($Sex); } # Customers date of birth input print ("Please enter your date of birth (DD/MM/YYYY): "); $Dateofbirth = <STDIN>; chomp ($Dateofbirth); if ($Dateofbirth !~/(\d{2}[-\/]\d{2}[-\/]\d{4})/) { print ("Please enter your birthdate in the following format DD/MM/YYYY: "); $Dateofbirth = <STDIN>; chomp ($Dateofbirth); } # Customer vision measurement print ("Please enter your vision measurement (between 1-99): "); $Vision = <STDIN>; chomp ($Vision); while ($Vision <1 || $Vision >99) { print ("Please enter a number between 1 and 99: "); $Vision = <STDIN>; chomp ($Vision); } print FILE ($CustRef,$Title,$Surname,$Forename,$Sex,$Dateofbirth,$Vision); close File; many thanks in advance the womblebat Posted by admin (Graham Ellis), 28 June 2007 changeprint FILE ($CustRef,$Title,$Surname,$Forename,$Sex,$Dateofbirth,$Vision); into print FILE ("$CustRef,$Title,$Surname,$Forename,$Sex,$Dateofbirth,$Vision\n"); I'm 10 minutes late for a meeting .... if you need full explanation, please let me know and I'll get back later. By the way - close File; should be close FILE;. Perl is case SeSiTiVe Posted by womblebat (womblebat), 28 June 2007 thanks Graham that worked a treat, felt such a prat at a simple code error ![]() now my problem (they never end do they) is that when a matched ref code is entered i need the program to pull the file from the .txt file and display it on screen, unmatched are entering smoothly into the .txt file, punctuated and newlined. here is the code i'm working with, any pointers would be greatly appreciated #!c:\perl\bin\perl.exe open FILE, ">>CustData.txt" or die $!; # Customer Reference Input print ("Please Enter Your Four Digit Reference Number: "); $CustRef = <STDIN>; chomp ($CustRef); while ($CustRef !~ m/^[0-9]{4}$/) { print ("Incorrect Please Try Again:\n"); $CustRef = <STDIN>; chomp ($CustRef); } $file = "CUSTDATA.txt"; if(-e $file) { open (FILE,"<CUSTDATA.txt") or die "Could not open file."; pwd(); } else { open (FILE,">CUSTDATA.txt") or die "Could not open file."; close (FILE); pwd(); } $numOfMatches = 0; while (<FILE>) { foreach ($_) { if ($_=~ m/^$password/) { print ("$_"); $numOfMatches++; Last; } } } # Customer Title Input @Title = ("Dr","Lady","Lord","Miss","Mr","Mrs","Ms","Sir"); print ("Please Enter Your Title: "); $Title = <STDIN>; chomp ($Title); foreach (@Title) { if ($Title !~/(Dr|dr|Lady|lady|Lord|lord|Miss|miss|Mr|mr|Mrs|mrs|Ms|ms|Sir|sir)/) { print("Incorrect Title! Please choose from the following list:\n"); print("$_@Title\n"); $Title = <STDIN>; chomp ($Title); } } # Customer Surname Input print ("Surname: "); $Surname = <STDIN>; chomp ($Surname); while ($Surname !~/(^[A-Za-z]{1,20})$/) { print ("Please enter your surname in alphabetic characters,max of 20 characters please: "); $Surname = <STDIN>; chomp ($Surname); } # Customer forename input print ("Forename: "); $Forename = <STDIN>; chomp ($Forename); if ($Forename !~/(^[A-Za-z]{1,20})$/) { print ("Please enter your forename in alphabetic characters,maximum of 20 characters please: "); $Forename = <STDIN>; chomp ($Forename); } # Customer gender input print ("Sex (M-male or F-female): "); $Sex = <STDIN>; chomp ($Sex); while ($Sex !~/(M|F|m|f)/) { print ("Please enter M for Male or F for Female: "); $Sex = <STDIN>; chomp ($Sex); } # Customers date of birth input print ("Please enter your date of birth (DD/MM/YYYY): "); $Dateofbirth = <STDIN>; chomp ($Dateofbirth); if ($Dateofbirth !~/(\d{2}[-\/]\d{2}[-\/]\d{4})/) { print ("Please enter your birthdate in the following format DD/MM/YYYY: "); $Dateofbirth = <STDIN>; chomp ($Dateofbirth); } # Customer vision measurement print ("Please enter your vision measurement (between 1-99): "); $Vision = <STDIN>; chomp ($Vision); while ($Vision <1 || $Vision >99) { print ("Please enter a number between 1 and 99: "); $Vision = <STDIN>; chomp ($Vision); } print FILE ("$CustRef,$Title,$Surname,$Forename,$Sex,$Dateofbirth,$Vision\n"); close FILE; thanks once again for your time and patience the womblebat Posted by admin (Graham Ellis), 29 June 2007 I don't fully understand what your question is (sorry!) so it's kinda hard to answer. But I have had a glance through your code and I notice ...Code:
Which appears (if a file does not exist) to create it, close it .... and then try to read from the closed file. No ... I think there's a possible issue with the logic in that ![]() I also notice many similar blocks of code where you're prompting for value, reading, validating, looping ... really you should be using a "sub" to contain the common code, with parameters passing in the pattern to match against and the prompt. That will reduce your code to blocks of easily managable size (such that I and others here can well help with any further questions) and will also mean that any bugs / changes in how that code works only need one set of fixes in the future and not "a dozen or more". If you're not familiar with this "sub" stuff and happen to be around our way, I can show you sometime - just pop down one evening. We can also help you take it much further - see http://www.wellho.net/course/ppfull.html which refers you to my bread and butter daytime job! This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |