Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Re: Reading data from a delimited flat file

by cjf (Parson)
on Jun 12, 2002 at 14:00 UTC ( [id://173826]=note: print w/replies, xml ) Need Help??


in reply to Re: Reading data from a document.txt
in thread Reading data from a document.txt

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://173826]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-03-28 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found