Re: Need help with a question.




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
.



Relevant Pages

  • Re: Need help with a question.
    ... When I rapped the code into a Sub I ... syntax error at test.pl line 7, ... I feel really dumb because it took me 5 minutes to figure out. ... Perl is case sensitive. ...
    (comp.lang.perl.misc)
  • Re: Syntax error in INSERT INTO statement.
    ... It looks like you're trying to concatenate fields together. ... execute this INSERT. ... > End Sub ... > Exception Details: System.Data.OleDb.OleDbException: Syntax error in> INSERT INTO statement. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Strange syntax error
    ... I get a syntax error I can't explain. ... sub openFile() ... Removing or commenting the openFile sub. ... the Switch module uses a source filter to do its thing. ...
    (comp.lang.perl.misc)
  • Re: Strange syntax error
    ... I get a syntax error I can't explain. ... sub openFile() ... Removing or commenting the openFile sub. ... Switch seems to be incompatible with mod_perl under ...
    (comp.lang.perl.misc)