Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Need help with a question.

Reply
Thread Tools

Need help with a question.

 
 
Trev
Guest
Posts: n/a
 
      06-28-2008
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;
 
Reply With Quote
 
 
 
 
Erwin van Koppen
Guest
Posts: n/a
 
      06-28-2008

"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...



 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-28-2008
Trev wrote:
> How come these errors only appear when I use Sub { } ?


Maybe because Perl is case sensitive.

s/Sub/sub/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Trev
Guest
Posts: n/a
 
      06-28-2008
On Jun 28, 12:45*pm, "Erwin van Koppen" <(E-Mail Removed)>
wrote:
> "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...


Thanks, I've made the changes and so far so good.
 
Reply With Quote
 
Erwin van Koppen
Guest
Posts: n/a
 
      06-28-2008

"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...




 
Reply With Quote
 
Trev
Guest
Posts: n/a
 
      06-28-2008
On Jun 28, 1:01*pm, "Erwin van Koppen" <(E-Mail Removed)>
wrote:
> "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...


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.
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-28-2008

Quoth Trev <(E-Mail Removed)>:
> 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.
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Trev
Guest
Posts: n/a
 
      06-28-2008
On Jun 28, 1:40*pm, Ben Morrow <(E-Mail Removed)> wrote:
Thanks Ben for the detailed reply, I've been able to get the output
correct, but I'm stuck on how to read $cpqlogline into an array
without writing it to a file.

my files look like this:

the perl file:

use warnings;
use strict;

my $server_file = "list.txt";
my $mactmp = "mactmp.txt";
my $maclog = "logfile.txt";

sub LoadFile
{
open(my $SRV, '<', $server_file)
or die("Could not open file: $!");

while (my $server = <$SRV>) {
chomp($server);
open (my $DAT, '<', 'output.txt')
|| die("Could not open 'output.txt': $!");

open (OTF, ">blah.txt");
print OTF $DAT;

while (my $cpqlogline = <$DAT>) {
{
chomp($cpqlogline);
if ($cpqlogline =~ /MAC/)
{
$cpqlogline =~ s/ <FIELD NAME="Subject" VALUE="//i;
$cpqlogline =~ s/ <FIELD NAME="MAC" VALUE="//i;
$cpqlogline =~ s/"\/>//i;

open (TMP, ">>$mactmp");
print TMP "$server". "," . "$cpqlogline\n";
close TMP;
}
}
}
}
close OTF;
}

sub CreateLOG
{
open (TMP, "<$mactmp");
open (LOG, ">>$maclog");
my @lines=<TMP>;
print LOG "$lines[1]";
close TMP;
close LOG;
print @lines;


unlink "blah.txt";
#unlink $mactmp;
}

LoadFile;
CreateLOG;

The list.txt file:

server1
server2
server3

The output.txt file:

<FIELD NAME="Subject" VALUE="Embedded NIC MAC Assignment"/>
<FIELD NAME="Port" VALUE="1"/>
<FIELD NAME="MAC" VALUE="00-00-00-00-00-01"/>
<FIELD NAME="Port" VALUE="2"/>
<FIELD NAME="MAC" VALUE="00-00-00-00-00-02"/>
<FIELD NAME="Port" VALUE="iLO"/>
<FIELD NAME="MAC" VALUE="00-00-00-00-00-03"/>

When the script runs it only creats the logfile with one line:
server1,00-00-00-00-00-01

When it should show:
server1,00-00-00-00-00-01
server2,00-00-00-00-00-01
server3,00-00-00-00-00-01

I see the problem is that the CreateLog only reads value[1] since the
above sub writes everything into mactmp.txt then only CreateLog is
run.

Any ideas?
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-28-2008
Trev <(E-Mail Removed)> wrote:
>On Jun 28, 1:40*pm, Ben Morrow <(E-Mail Removed)> wrote:
>Thanks Ben for the detailed reply, I've been able to get the output
>correct, but I'm stuck on how to read $cpqlogline into an array
>without writing it to a file.


I suppose with "read into an array" you meant storing the value in an
array. Please see e.g. "perldoc -f push".

However, there is no need for that. Why not write the output file line
by line simultaneously as you process the input file line by line?

jue
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-28-2008
Anonymous coward <(E-Mail Removed)> wrote:
>On Sat, 28 Jun 2008 13:18:08 -0700, Trev wrote:
>> open (OTF, ">blah.txt");
>> print OTF $DAT;

>
>That makes no sense at all. Why would you want to print a filehandle


Maybe he wants to print _to_ a filehandle?

>> open (TMP, ">>$mactmp");
>> print TMP "$server". "," . "$cpqlogline\n"; close TMP;

>
>Why do you open and close a file inside a loop?


Indeed, that is an expensive operation that is better done once outside
of the loop.

>Why don't you just say
>print "$server,$cpglogline\n" ?


Mabye because (unless you set the default filehandle using select()) the
text will end up on the screen instead of in the file?

>This program makes absolutely no sense to me. What are you trying to do
>exactly? Can you give a bit more description?


It is certainly somewhat cryptic.

jue
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help, I am intermediate in Java...need help in follow case ElementX Java 9 10-01-2008 08:02 PM
Help Help. I really need some help with this =?Utf-8?B?Q2hyaXM=?= ASP .Net 3 01-31-2007 09:33 PM
re_---need help Network Adapters!!!! NEED HELP!!!! hedayatniac@gmail.com Computer Support 4 08-13-2006 01:03 AM
Need help! I need to add lead zeros to a textbox Teep ASP .Net 2 06-21-2004 01:04 PM
Please help!!! Need datagrid selection to fill textboxes...Need quick!! TN Bella ASP .Net 1 06-18-2004 01:31 AM



Advertisments