Hi i am trying to write a perl script to save a form to a file. Can anyone help?
Saving a Form to a file
Collapse
X
-
Sure, but we have to know things like:
- what you have tried thus far
- what errors you are seeing
- what is not working
If you can post your code (surrounded by code tags please) and provide us the other information, we will do our best to help you.
Regards,
Jeff -
i was able to save it to a file but now i just want to read in the file and just print out the data on a new line for each element. here is the code: i want the output to look exactly like how it is saved in the file
*************** *****data from file*********** *************Code:#!/usr/bin/perl use strict; use CGI qw(:standard); use CGI qw(:cgi); my $data_file = "/tmp/results.txt"; my $configuration; my $config_ID; my $cds_ID ; open (DAT, $data_file) || die ("could not open file"); my @raw_data = <DAT>; close(DAT); print "Content-type: text/html \n\n"; print "<html><body>"; print "(@raw_data)"; print "</body></html>";
txtCfgId=test
txtCfguser=test
txtEmailAdr=
txtPager=test
txtSrvOU=test
txtProject=rhga
pnlServerID=eag a
txtSrvrIP=egar
txtSrvrGW=agae
txtSrvrMask=eag a
lstTimeZone=US% 2FCentral
txtDNSDom=aega
txtDNSHold1=efa r
txtDNSHold2=erg ar
txtDNSHold3=Comment
-
nmeliasp,
First, the code tags that I asked you to use to surround your code were not optional. They are required for the forums and your not putting them in requires a moderator to add them for you. Please add them to your code next time, as I requested earlier!
As for your issue, the topic of your post seems to be misleading. It is saying that you want to save a form to a file yet in your latest post, you are wanting to display the contents of a file on the screen, as it appears in the file.
When you run what you have, how does the output look?
Personally, in place of this line:
I would write it as so, but this is just me:Code:print "(@raw_data)";
I didn't test it, but that should print out the file, line by line, making it look on the screen as it did in the file.Code:foreach my $line (@raw_data) { chomp($line); print("$line\n"); }
Regards,
JeffComment
-
this is the output of the code in an internet browser which is what i want the output to be in:
VTI-GROUP=0 txtCfgId=test txtCfguser=test 1 txtEmailAdr=nme liasp%40yahoo.c om txtPager=test txtSrvOU=test txtProject=rhga pnlServerID=eag a txtSrvrIP=egar txtSrvrGW=agae txtSrvrMask=eag a lstTimeZone=US% 2FCentral txtDNSDom=aega txtDNSHold1=efa r txtDNSHold2=erg ar txtDNSHold3= Submit=Submit =Comment
-
He he, I didn't even take that into account, sorry, long day. Try this and see if it works:Originally posted by nmeliaspit was the same when i tried that piece of code you suggested
Let me know if that works.Code:foreach my $line (@raw_data) { chomp($line); print("<p>$line</p>"); }
Regards,
JeffComment
-
Output in a browser requires HTML markup to display properly. Jeff posted a simple example but this is not the HTML forum. If you need to learn the basics of HTML find an HTML tutorial or read the CGI.pm modules documentation for generating HTML output.Originally posted by nmeliaspthis is the output of the code in an internet browser which is what i want the output to be in:
VTI-GROUP=0 txtCfgId=test txtCfguser=test 1 txtEmailAdr=nme liasp%40yahoo.c om txtPager=test txtSrvOU=test txtProject=rhga pnlServerID=eag a txtSrvrIP=egar txtSrvrGW=agae txtSrvrMask=eag a lstTimeZone=US% 2FCentral txtDNSDom=aega txtDNSHold1=efa r txtDNSHold2=erg ar txtDNSHold3= Submit=Submit =
Jeff,
this is not a good example:
Code:foreach my $line (@raw_data) { chomp($line); print("$line\n"); }
FIrst it makes little sense to chomp() the input only to add a newline to print the output. But the bigger issue is the use of parenthesis around the argument to the print command. That is really almost never needed, although there are times, when it is, like this:
but if you do that it will print nothing, it will in fact throw a syntax error . You have to use some odd syntax to make it work:Code:print (split(//))[0],"\n";
or:Code:print ((split(//))[0]),"\n";
But in this example the newline will not be printed:Code:print +(split(//))[0],"\n";
because the print function expects a list, and the parenthesis create a list, so after the last parenthesis the list ends and the newline is ignored. You have to put the newline inside the parenthesis to make it part of the list. In this example it will print the newline:Code:print ((split(//))[0]),"\n";
But that is rather obscure syntax and to the unsuspecting perl coder it will look like it should not even work. Hell, I don't even understand why it works but its documented in the print functions manpage.Code:print +(split(//))[0],"\n";
The best practice is to avoid using parenthesis around the arguments of the print function unless it is really necessary.
Damn, I drank too much coffee this morning.Comment
Comment