Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please Help * Perl Script Problem

Status
Not open for further replies.

JRMS

MIS
Sep 4, 2003
144
0
0
US
Please help me with my script please. I am trying to do the following:

1. Read files for the current directory
2. Open and read from nbe files files only
3. Read only the lines with the results pattern
4. Split the line and print 3rd field

Please indicate what line I need to modify. Thanks a bunch.

1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4
5
6 # Open the current directory
7 my $directory = '.';
8
9 # Read from the current directory
10 opendir (DIR, $directory) or die "Folder not found: $1";
11
12
13 while (my $file = readdir(DIR)) {
14
15 if (my $File =~/\.nbe$/){
16
17 open (File, my $File) or die "Could not open NBE file:";
18
19 while (my $line = <FILE> )
20
21 if ($line =~/results/)
22
23 our @$values = split (/\|/, $line);
24
25 print my $values[3];
26 }
27
28 }
29
30 close(DIR);


Error Message:


syntax error at ./perlnbe_v2.pl line 30, near ")

if"
syntax error at ./perlnbe_v2.pl line 34, near "$values["
Execution of ./perlnbe_v2.pl aborted due to compilation errors.
Edit/Delete Message
 
You're missing the curly brackets for

Code:
19 while (my $line = <FILE> )
20 {
21   if ($line =~/results/)
22     {
23       our @$values = split (/\|/, $line);
24
25       print my $values[3];
       }
   }

------------------------------------------
There are 10 kinds of people in this world. One that understands binary and the other one that does not.
 
get rid of the my after the if, decide if you are using File or file, add $! to your die messages.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Hi!
In PERL you must take always {} when use if.
You forget this at line 21.:if ($line =~/results/)
(When you tabulate your program youcan see that things easy)
I hope it help you!
 
Thanks for your assistance. I have made the modifications; however, still receive the following errors:


"my" variable @values masks earlier declaration in same scope at ./perlnbe.pl line 25.
syntax error at ./perlnbe.pl line 25, near "$values["
Execution of ./perlnbe.pl aborted due to compilation errors.

#!/usr/bin/perl -w
use strict;
use warnings;


# Open the current directory
my $directory = '.';

# Read from the current directory
opendir (DIR, $directory) or die "Folder not found: $!";


while (my $File = readdir(DIR)) {

if ($File =~/\.nbe$/){

open (File, my $File) or die "Could not open NBE file:";

while (my $line = <FILE> )
{
if ($line =~/results/)
{
our @values = split (/\|/, $line);

print my $values[3];
}
}
}

I would appreciate any recommendations. :eek:

 
this line:

open (File, my $File) or die "Could not open NBE file:";


should be:

open (File, $File) or die "Could not open NBE file:";



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Well, you use brackets wrong again .When you count them youcan see 4 left({) but just 3 right(}) brackets .So your first while loop "doesnt has end".
 
Try:

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


# Open the current directory
my $directory = '.';

# Read from the current directory
opendir (DIR, $directory) or die "Folder not found: $!";


while (my $File = readdir(DIR)) {

	if ($File =~/\.nbe$/){

		 open (File, $File) || die "Could not open NBE file:";

			while (my $line = <FILE> ) {
				 if ($line =~/results/) {
					our @values = split (/\|/, $line);
					print my $values[3];                                }
				 }
			}

		 close(File);

	}

}

for good practive, try and indent your sections, so you can see what brackets actually go with what ;)

Cheers

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top