Click here to get back home

Need help with a question.

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Need help with a question. Trev 06-28-2008
Posted by Trev on June 28, 2008, 12:33 pm
Please log in for more thread options
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;
use Warnings;

Sub LoadFile
{
        open (DAT, "<output.txt") || die("Could not open file!");
        @cpqlog_data=<DAT>;

                foreach $cpqlog (@cpqlog_data)
                        {
                                {
                                        chomp($cpqlog);

                                        if ($cpqlog =~ /MAC/)
                                        {
                                                $cpqlog =~ s/ <FIELD NAME="Subject" VALUE="//i;
                                                $cpqlog =~ s/ <FIELD NAME="MAC" VALUE="//i;
                                                $cpqlog =~ s/"\/>//i;

                                        }
                                }
                        }
        close DAT;
}

Sub CreateLOG
{
        open (BOO, "<blah2.txt");
        @lines=<TMP>;
        print $lines[1];
        close BOO;
}

LoadFile;
CreateLOG;

Posted by Erwin van Koppen on June 28, 2008, 12:45 pm
Please log in for more thread options

"Trev" wrote:
>
> How come these errors only appear when I use Sub { } ?

For one thing, you should use lowercase: sub { }

> open (BOO, "<blah2.txt");
> @lines=<TMP>;

You open BOO, but use TMP (which is not opened).

And, you don't check to see of BOO can be opened.

Fix these things, then it should run. However, I doubt it will do what you
want it to do...




Posted by Trev on June 28, 2008, 12:56 pm
Please log in for more thread options
wrote:
> "Trev" wrote:
>
> > How come these errors only appear when I use Sub { =A0 } ?
>
> For one thing, you should use lowercase: sub { }
>
> > open (BOO, "<blah2.txt");
> > @lines=3D<TMP>;
>
> You open BOO, but use TMP (which is not opened).
>
> And, you don't check to see of BOO can be opened.
>
> Fix these things, then it should run. However, I doubt it will do what yo=
u
> want it to do...

Thanks, I've made the changes and so far so good.

Posted by Erwin van Koppen on June 28, 2008, 1:01 pm
Please log in for more thread options

"Trev" wrote:
>
> Thanks, I've made the changes and so far so good.

foreach $cpqlog (@cpqlog_data) {
{
chomp($cpqlog);

if ($cpqlog =~ /MAC/) {
$cpqlog =~ s/ <FIELD NAME="Subject" VALUE="//i;
$cpqlog =~ s/ <FIELD NAME="MAC" VALUE="//i;
$cpqlog =~ s/"\/>//i;
}
}
}

You do understand that the above code does not actually change anything in
the file, right? The substitutions in $cpqlog are just discarded at the end
of the loop.

Oh, and you might want to get rid of the superfluous { }'s...





Posted by Trev on June 28, 2008, 1:12 pm
Please log in for more thread options
wrote:
> "Trev" wrote:
>
> > Thanks, I've made the changes and so far so good.
>
> foreach $cpqlog (@cpqlog_data) {
> =A0 =A0 {
> =A0 =A0 chomp($cpqlog);
>
> =A0 =A0 if ($cpqlog =3D~ /MAC/) {
> =A0 =A0 =A0 =A0 $cpqlog =3D~ s/ =A0<FIELD NAME=3D"Subject" VALUE=3D"//i;
> =A0 =A0 =A0 =A0 $cpqlog =3D~ s/ =A0<FIELD NAME=3D"MAC" VALUE=3D"//i;
> =A0 =A0 =A0 =A0 $cpqlog =3D~ s/"\/>//i;
> =A0 =A0 }
> =A0 =A0 }
>
> }
>
> You do understand that the above code does not actually change anything i=
n
> the file, right? The substitutions in $cpqlog are just discarded at the e=
nd
> of the loop.
>
> Oh, and you might want to get rid of the superfluous { }'s...

Yeah, I figured that out, I added a print BOO so the results are
writen to a file which I load in the next sub. This might not be the
best code but I'm still learning.

Similar ThreadsPosted
I want to ask you the most important question of your life. The question is: Are you saved? It is not a question of how good you are, nor if you are a church member, but are you saved? Are you sure you will go to Heaven when you die? The reason some peopl May 22, 2005, 10:11 am
I want to ask you the most important question of your life. The question is: Are you saved? It is not a question of how good you are, nor if you are a church member, but are you saved? Are you sure you will go to Heaven when you die? The reason some peopl May 24, 2005, 8:49 am
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you are, April 21, 2005, 1:25 pm
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you are, April 22, 2005, 3:33 pm
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you are, April 24, 2005, 6:50 pm
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you are, May 1, 2005, 11:27 am
MS Perl question -- how to use hacked script to work correctly(was Question on loops and return values or sumpin) December 8, 2004, 12:59 pm
Question September 9, 2004, 11:05 am
die question September 14, 2004, 7:27 pm
use CGI question October 14, 2004, 10:11 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap