473,569 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question on files and arrays

1 New Member
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 2557
miller
1,089 Recognized Expert Top Contributor
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
2173
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. I'm curious about the most efficient way to do this. I don't want to query the database separately for each file as this would be horribly...
7
11761
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
4254
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) and puts values in a number of arrays and then puts the contents of the array in a HTML table. I'd like to keep the array structure. I've checked...
18
1575
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: ========================= typedef struct fubar
35
2688
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 I'm pretty sure the rest of the program is working just fine. The only thing I could think might be wrong is that the if statement can only hold so...
2
2243
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 far, it works great (after having a bit of heartache about .Dispose() ). Now, I need a certain part of the class file to run through many times and
21
3179
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 column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a...
24
3413
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 declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
7
6414
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 now thown out of the array released properly by the CLI?
10
2462
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 struct in the header file and including this file in all source files that need it? For example: mystruct.h
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.