473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

question on files and arrays

1
i hope this is a simple answered question
this program has been annoying me for some time, its pretty much the first thing i have attempted to do in perl. its a pretty simple guestbook type thing. it all works ok in that it saves all the newest information to a log file. however in falls down in the whole putting-the-old-data-back part. which is really quite a desirable function in my script.
at the moment i have only four fields. the script is supposed to read the old data into an array, print the new data into the log file, then print the old data back after it, keeping the newest entries at the start of the file. eventually i plan to print the entries in all formatted in html then just use an ssi to paste the log file into another page. anyway! the script reads something out of the log file, and prints the new fields in no problem, however it only prints in the first line of the previous log file.
so the log file only ever has 5 lines really.
i have read asmany pages of perl documentation i can handle without breaking something, and they al seem to be telling me to do what i allready have.
i have checked my server error log and nothing shows up. so its not really an error, just my code doing the rong thing.
well here it is

Expand|Select|Wrap|Line Numbers
  1. #!/perl/bin/perl -wT
  2. use CGI qw(:standard);
  3. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  4. use Fcntl;
  5.  
  6. #assign web form to array:form
  7. my %form;
  8. foreach my $p (param()) {
  9.     $form{$p} = param($p);
  10. }
  11.  
  12. #open guest file for read/write, read data into array, set pointer to start of file
  13. sysopen (GUESTFILE, "guests.txt", O_RDWR | O_CREAT)                 or die "Couldn't open for reading: $!\n";
  14. my @data;
  15. @data = <GUESTFILE> || 0                            or die "cant read from file $!\n";
  16. seek(GUESTFILE, 0, 0)                            or die "cant rewind file $!\n";
  17.  
  18. #print fields to file
  19. print GUESTFILE "First Name: $form{first_name} \n"                or die "cant print first to file$!\n";
  20. print GUESTFILE "Last Name: $form{last_name} \n"                or die "cant print second to file $!\n";
  21. print GUESTFILE "Country: $form{country} \n"                    or die "cant print country to file$!\n";
  22. print GUESTFILE "Email: $form{email} \n"                    or die "cant print email to file $!\n";
  23.  
  24. #print earlier entries into file
  25. foreach $line (@data) {
  26.     print GUESTFILE ("$line")                         or die "cant print old file to file $!\n";
  27. }
  28.  
  29. #close guest file
  30. close (GUESTFILE);
  31.  
  32.  
  33. #print html document to page, thank user, return to form page
  34. print "Content-type: text/html\n\n";
  35. print "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2//EN'>";
  36. print "<html><head><title>Form Response Page</title>";
  37. print "<META HTTP-EQUIV='REFRESH' CONTENT='5; URL=index.html'>";
  38. print "</HEAD><BODY bgcolor = '9900cc'>";
  39. print "<font color = ' ff0000'><h1><center>Thankyou! Your Guestbook Entry Has Been Added!<br>\n";
  40. print "Please Wait To Be Redirected To The Main Guestbook Page<br>\n";
  41. print "Or If You Are Impatient...<br>\n";
  42. print "<a href = \"index.html\">Click Here To Go Back To The Guestbook Page</a> \n";
  43. print "</body></html>"
i can only assume theres something im doing rong in either
Expand|Select|Wrap|Line Numbers
  1. @data = <GUESTFILE> || 0
or
Expand|Select|Wrap|Line Numbers
  1. foreach $line (@data) {
  2.     print GUESTFILE ("$line")                         or die "cant print old file to file $!\n";
  3. }
?
or is there something im missing?
something obvious probably.
thanks in advance for absolutley any help =]
Oct 9 '06 #1
1 2544
miller
1,089 Expert 1GB
Yes, you're right, this is easily fixed. The follow line of your code is your problem:

Expand|Select|Wrap|Line Numbers
  1. my @data;
  2. @data = <GUESTFILE> || 0 or die "cant read from file $!\n";
  3.  
The "|| 0" forces the read of the guest file into a scalar context, and so only a single line is read. I cannot think of a reason why this code is included, so it should just be moved, along with the "or die" statement.

Expand|Select|Wrap|Line Numbers
  1. my @data = <GUESTFILE>;
  2.  
Oct 16 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: John Hoge | last post by:
I'm looking to make an admin page for a web photo gallery, and I need to make a drop down menu showing all files in a given directory that do not have comments in a database for the given filename....
7
by: csx | last post by:
Hi everyone! two quick questions relating to arrays. Q1, Is it possible to re-assign array elements? int array = {{2,4}, {4,5}}; array = {2,3}
4
by: annoyingmouse2002 | last post by:
Hi there, sorry if this a long post but I'm really just starting out. I've been using MSXML to parse an OWL but would like to use a different solution. Basically it reads the OWL (Based on XML)...
18
by: xarax | last post by:
Greetings, What is the general practice, usual and customary way, of including a data file into a source file? I have some large data structures defined as source similar to: ...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
2
by: David | last post by:
Hi all, I am fairly new to C#. so go easy on me :-) Anyhow, I have a class file that I have set up properties and a method. I am calling this class file directly from and aspx.cs file. So...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
10
by: Tammy | last post by:
Hello all, I am wondering what is the best way to declare a struct to be used in other c and c++ files. Such as for a C API that will be used by others. 1. Declaring the typedef and the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.