Re: Reading data from a document.txt
by cjf (Parson) on Jun 12, 2002 at 13:48 UTC
|
| [reply] |
|
|
Right, here is all my code:
FROM THE HTML DOC, the variables are:
<HTML>NAME, SURNAME, AGE, SEX, COMMENTS
action form: POSTIT.PL </HTML>
=======
POSTIT.PL
#!/usr/local/bin/perl
my $nam = param ('name');
my $srn = param ('surname');
my $sex = param ('sex');
my $age = param ('age');
open (LOG, ">>messages.txt");
print LOG "$nam $srn $sex $age";
close LOG;
=========
from my READIT.PL
#!/usr/bin/perl
open (LOG, "<messages.txt")
while (<LOG>) {
chomp;
my @attributes = split /\|/;
for my $attribute (@attributes) {
print $attribute, "\n"; # or do whatever here
}
}
close LOG;
======
THAT'S ALL THE CODE I'VE GOT SO FAR!
| [reply] [d/l] [select] |
|
|
I'll just assume you meant to post this under Reading data from a document.txt.
You seperate your attributes with a space, and then try to get them back from the file splitting on a pipe character. Also you print all entries on the same line.
Try something like:
tr/|/ / for ($nam,$srn,$sex,$age); # remove | characters
print LOG "$nam|$srn|$sex|$age\n"; # print seperated by |
In your POSTIT.PL
And please, please, please, read How (Not) To Ask A Question or people might get upset with you.
--
Joost downtime n. The period during which a system
is error-free and immune from user input.
| [reply] [d/l] [select] |
|
|
- Are you running these scripts on different machines?
They show perl as being in different places.
- Add a -w to your #! lines.
- use strict;
- Indenting your code makes it much easier to read.
- You didn't tell us what you want to accomplish, so we
can't help you do it.
- You forgot to ask a question.
| [reply] [d/l] |
|
|
Re: Reading data from a document.txt
by cacharbe (Curate) on Jun 12, 2002 at 13:48 UTC
|
Some excellent questions. We would be able to help you more if we knew:
- What the text file looked like - post an example
- What you have tried yourself so far - post an example
C-. | [reply] |
|
|
What the text file looked like
I'll take a wild guess and say it's a pipe-delimited flat file, similar to this ;-).
If this is the case, what we need to know is what do you want to do with it?
Update: Also, are you sure the data was properly stored to begin with? Have you opened up the text file and checked if the data is both there and in the proper format?
Another Update: Based on what you posted here , the following should work:
In postit.pl...
#!/usr/bin/perl -wT
use strict;
use CGI;
my $q = new CGI;
my $name = $q->param('name');
my $surname = $q->param('surname');
my $sex = $q->param('sex');
my $age = $q->param('age');
my $string = join('|', $name, $surname, $sex, $age);
open LOG, ">>messages.txt" or die "Can't open file: $!";
print LOG $string, "\n";;
close LOG;
And in readit.pl...
#!/usr/bin/perl -wT
use strict;
open LOG, "messages.txt" or die "Can't open file: $!";
while (<LOG>) {
chomp;
my @attributes = split /\|/;
for my $attribute (@attributes) {
print $attribute, "\n"; # or do whatever here
}
}
close LOG;
Now in postit.pl you should perform checks on the size of the parameters. You may also want to consider combining the two scripts and having separate modes (one read_data sub and one write_data sub maybe).
Yet Another Update: Guess I should explain the changes I made to your code:
- Added warnings, strict, and a few dies if your open calls fail. These will save you a lot of debugging time.
- Added taint mode (-T flag). You should include this in all your CGI scripts. See Essential CGI Security Practices for more details.
- Added CGI.pm to handle the parameter parsing. See Use CGI or die; for more info.
- Added the join before printing to the file.
Feel free to post a follow up if you have any more questions. | [reply] [d/l] [select] |
Re: Reading data from a document.txt
by demerphq (Chancellor) on Jun 12, 2002 at 13:54 UTC
|
Hi,
Well, its good to see you come over from castro-land. Welcome. The only problem with coming here and asking questions is that unlike in castro land we wont make stuff up. So for instance your question has no meaningful content by which we can determine what the problem you are having is, let alone what the solution should be.
Basically you are going to need to provide us a with the relevant part of your code that writes the data out (dont post your whole script please) and the part of the script that reads it in. Also a small snippet of (possibly censored/munged) your data would be useful. Please use CODE tags around the code that you post. (A helpful little summary of usable tags should be visible below the message post form.)
Without giving us some basic information you arent going to get much better than a sarcastic answer: probably something like "Hmm, maybe its cause you havent changed the batteries in your goldfish?"
The people here at the monastery are a very helpful bunch so if you give some good information to go on and good reason to beleive that they are helping and not doing then you'll get a good answer in a jiffy I promise.
BTW there are a couple of site documents you probably should read, but start with this one:How (Not) To Ask A Question
Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look. | [reply] |
Re: Reading data from a document.txt
by vladb (Vicar) on Jun 12, 2002 at 13:51 UTC
|
I can't get the data the way the user typed it in!
What do you mean? If the user typed his message in a plain text, there shouldn't be any difficutly displaying it back the way it was typed. Are you dealing with some sort of custom formatting? Could you please tell us more about your problem and probably include the script you've got so far.
For the record, I should mention that there are already zillions of free scripts (in Perl) that can do the job awesomely for you. Unless you want to learn how to do it on your own, I'd suggest looking through this google search results and see if there's any readily available script that you might like.
_____________________
open(I,$0);<I>;$~=$/;$/='~';$_=<I>;($/)=/(.)$~/;s/[$~~]//g;/(v)/;$-=$-[0];s;\Q$/\E;$~;g;($/,$^)=/^(.)(.)/;
#% xxxxxx xx-+ xx xxx xx xx xx xx xxx xxxxx+ xx xx xxxx xxxxx ......+
#x xxxxvxxx xx xx xv xxxx x+ %+ ===== xx xx xx xx x+ =x xx xx xx xx xx ...+
#x xx xx xx xx xx xx xx xx xxx+ xxxxxx xx +x xx xx+x- xxxx xxxx ........+
#% xx xx xx xx xx xx xx x+ xx xx =+=== xx xx xxxx-xx xx =x +x xx xx xx xx ...+
#% xx xx xx -+x+ xxx+ xx xx xx xx xx x+ xx xxx+xx xx xx xxxx xx xx ....+~
for(split/$~/){s,[ $/],,g;/(.)$/;$l=$-[0];/(.)/||next;$_=chr$-+$l;$".=($1=~/$^/)?" \u$_":$_;}print$";
| [reply] |
|
|
| [reply] |
Re: Reading data from a document.txt
by orkysoft (Friar) on Jun 12, 2002 at 14:24 UTC
|
Obviously, your program doesn't work because it doesn't do what you intended it to do. That's all I can say without you providing more details, as other monks have explained to you already.
You say 'So far no problem' right after your description of the program that writes to DATA.TXT, so I could assume the data in that file looks good, right? Then you'll need to adapt your file-reading, data-displaying program to display the data correctly.
Alternatively, you could change your userdata-reading program so that it appends directly to an HTML file, which can be displayed by a browser directly, without needing to go through a second translation. It's easier, and since you sound pretty new to programming, a challenge better suited to you.
Lur: "But if this cape shrinks, consider your species extinct!"
| [reply] |
| A reply falls below the community's threshold of quality. You may see it by logging in. |