CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Whatis wrong with this code?

 



BrightNail
Novice

Jul 29, 2001, 10:31 PM

Post #1 of 6 (1022 views)
Whatis wrong with this code? Can't Post

can someone tell me why I am getting errors. the errors are vague and non-informative.


#!usr/local/bin/perl

$infile="datafile_back.txt";
$outfile="blank.txt";
open (F, "<$infile")or die("couldn't open the Datafile");
open (OUTFILE, ">$outfile")or die("couldn't open the Datafile");


while($line=<F>){
chomp($line);
if($line eq '----------------------------------'){
print OUTFILE
join('|',@current) . "\n";

@current = ();
} else {
if( $line ne '' ){
@ar = split(/|/,$line,2);
push @current, $ar[1];
}
}
}



abstracts
Novice

Jul 30, 2001, 3:26 AM

Post #2 of 6 (1016 views)
Re: Whatis wrong with this code? [In reply to] Can't Post

Hello,
There are some places where your code needs help. I'll point them out for you.

Code
#!/usr/local/bin/perl -w 
use strict;

my $infile="datafile_back.txt";
my $outfile="blank.txt";
open (F, "<$infile") or die("couldn't open the input file: $!"); # make sure the debug message informs
open (OUTFILE, ">$outfile") or die("couldn't open the output file : $!"); # you where it got stuck

# if you say while($line = <F>){, then the loop will terminate if it ever reads an empty line
# since empty strings evaluate to false. What you need to do is either use
# while (defined(my $line = <F>))
# or
# while(<F>)

my @current;
while(<F>){
chomp;
next unless length; # skip empty lines
if($_ eq '----------------------------------'){ # I'd go with if(/^-*$/) but that's just me
print OUTFILE join('|',@current) . "\n";
@current = ();
} else {
my @ar = split(/\|/,$_,2); #split into 2 parts on the first "|"
push @current, $ar[1]; # push the second part into the current array
}
}

Now I wouldn't be able to tell what you really wanted to achieve since you posted no explanation.
Providing more information really helps spotting the errors in your code (logical errors, not syntax errors).

One more thing is that you should really turn warnings on (-w) otherwise, you'll miss all the useful error messages.

Hope this helps,,,

Aziz,,,





BrightNail
Novice

Jul 30, 2001, 8:14 AM

Post #3 of 6 (1012 views)
Re: Whatis wrong with this code? [In reply to] Can't Post

thanks,
I['ll have a go at it...

I was trying to do what I posted previously...
Log into to a file (pipe deliminated) using the line
as a seperator.......see a line, grab contents after it till the next line.....see a new line,,,grab the contents after that particular line and funnel them to a flatfile.

thanks,



dsb
stranger

Jul 30, 2001, 8:42 AM

Post #4 of 6 (1010 views)
Re: Whatis wrong with this code? [In reply to] Can't Post

If you post the errors you are getting, someone can probably isolate your problem for you a lot better.

dan Wink


BrightNail
Novice

Jul 30, 2001, 9:38 AM

Post #5 of 6 (1007 views)
Re: Whatis wrong with this code? [In reply to] Can't Post

Just your standard 500 server error, nothing else beyond that. When I use other things to check the problem, I just get compilation errors....thanks,



sigma
stranger

Jul 30, 2001, 10:40 AM

Post #6 of 6 (1000 views)
HOW TO FIND AN ERROR. (Was: Whatis wrong with this code?) [In reply to] Can't Post

You should have a look at web server's error_log
(on unix tail -f /var/log/whenever/it/is/error-log will do that)

or try at least from command line:
perl -c your_script.cgi for syntax errors and/or
perl -w your_script.cgi for run-time errors. (If you are using CGI.pm it will ask for query)

I watch error log all the time, sometimes I need to catch warn and die:
$SIG{__WARN__} = sub { print STDERR @_; }
$SIG{__DIE__} = sub { print STDERR @_; }


--
for ($!=0;$!<123;$!++) { print "$!\n"; }

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives