PDA

View Full Version : Newbie help - Reading files into script


IceManJoe
11-27-2002, 01:26 PM
Hi,

I wonder if you could help me.

I would like to write a perl script that will reads in a text file that holds peoples fornames and surnames. If the forname or surname has a number in it, I would like it to list the error in a error text file. The text file may have comments in so i would like to overcome this problem too.

Any help would be great.

Joe

CodeWhacker
11-27-2002, 02:12 PM
Here's a sketch, assuming your comments are perl-style:
#!/usr/bin/perl
use Carp;
open(I,"<$filename") || die;
while(<I>) {
s/#.*$//;
next unless $_;
ComplainAboutError if /\d/;
DoSomethingWithNames();
}


Untested, top-of-the-head, YMMV, yada yada... :)

IceManJoe
11-27-2002, 02:29 PM
How would i get the errors to go into a error file?

IceManJoe
11-27-2002, 03:02 PM
I would like it to go to a error.txt file rather than the error on screen using CARP, any ideas?

CodeWhacker
11-27-2002, 04:16 PM
use CGI::Carp qw(carpout);

See the documentation (http://theoryx5.uwinnipeg.ca/CPAN/data/perl/CGI/Carp.html) .

IceManJoe
11-27-2002, 04:29 PM
Thanks for the advise.

Do you know how to write to a error.txt file instead?

CodeWhacker
11-27-2002, 10:19 PM
Sure I do. Read the documentation (http://theoryx5.uwinnipeg.ca/CPAN/data/perl/CGI/Carp.html#REDIRECTING_ERROR_MESSAGES)!

:p