Re: Need help with a question.
- From: Ben Morrow <ben@xxxxxxxxxxxx>
- Date: Sat, 28 Jun 2008 18:40:28 +0100
Quoth Trev <trevor.dodds@xxxxxxxxx>:
I'm having a problem with my Perl script, what I would like the script
to achieve is to read a file, search it for certain words, put the
results into an Array so I can then call each result with $var[1] etc
and print output to a file. I tried doing it without Sub routines but
wasn't able to split the results. When I rapped the code into a Sub I
get these errors:
syntax error at test.pl line 7, near "@cpqlog_data"
syntax error at test.pl line 24, near "}"
How come these errors only appear when I use Sub { } ?
test.pl
use English;
It's probably a bad idea to get into the habit of using English. Almost
noone who knows Perl well uses it, so you're going to have to learn the
punctation variables anyway; and once you've learned them, it's easier
to remember one list of special cases than two.
use Warnings;
Sub LoadFile
You've been told 'sub' is case-sensitive; 'warnings' is as well. If
you're on an OS with a case-insensitive filesystem, you need to be
particularly careful about the case of module names: loading a module
with the wrong case can have rather odd effects.
You also want
use strict;
here, and you need to declare your variables with 'my'.
{
open (DAT, "<output.txt") || die("Could not open file!");
Since you're just starting to learn Perl now, you *definitely* want to
get into the habit of using lexical filehandles right away; that is,
instead of 'DAT', use a real variable. You also want to use three-arg
open, and give the reason why the open failed:
open (my $DAT, '<', 'output.txt')
|| die("Could not open 'output.txt': $!");
I would use 'or' instead of '||', and omit the parens, but that's
entirely up to you.
@cpqlog_data=<DAT>;
foreach $cpqlog (@cpqlog_data)
This is not the most straightforward way to read a file. Since you're
processing it entirely line-by-line, read it line-by-line as well.
while (my $cpqlog = <$DAT>) {
{
If you use GNUish indenting, and such a large indent, you'll quickly run
out of screen room... :)
close DAT;
One of the advantages of lexical filehandles is that they close
themselves when they go out of scope. If you're not going to check the
return value of close (not generally necessary when reading a file),
it's much more convenient to omit it.
Once you've got the syntax errors sorted out, I presume you can start
working out the logic errors on your own... :) Don't be afraid to ask
again if you get stuck.
Ben
--
We do not stop playing because we grow old;
we grow old because we stop playing.
ben@xxxxxxxxxxxx
.
- Follow-Ups:
- Re: Need help with a question.
- From: Trev
- Re: Need help with a question.
- References:
- Need help with a question.
- From: Trev
- Need help with a question.
- Prev by Date: Re: Need help with a question.
- Next by Date: Re: change a single digit to corresponding English word
- Previous by thread: Re: Need help with a question.
- Next by thread: Re: Need help with a question.
- Index(es):
Relevant Pages
|