Perl Script Issue - Please Help * Thanks!!!

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.

All you need is:

perl -F'\|' -lane'
  print $F[2] if /results/
  ' *nbe

Or use sed (here you can use re-interval, if your sed supports it):

sed -n '
  /results/s/[^|]*|[^|]*|\([^|]*\).*/\1/p
  ' *nbe

Or AWK, or ...

Thanks for your response. My desire is to build on this script to eventually include other file format as well. Do you have any suggestions for the current script. --Thanks.

Something like this:

#! /usr/bin/perl

use warnings;
use strict;

my @Files = glob("*.nbe");
my $Pattern = 'results';

for (@Files) {
  open FH, $_ or warn "Cannot open file: $!" and next;
  while (<FH>) {
    print +(split /\|/)[2], "\n" if /$Pattern/;
    }
  close FH
  }

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. :o

The problem is you are using to many "my" variable. Declare the variable once with my. Then when accessing just use the variable name.

For instance

my $File = "/etc/passwd"
open (FILE, $File) or die "Can't open file"

That should correct most of you issues.

OK,
I understand, you only want to correct your own version. Try this:

#!/usr/bin/perl
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) or die "Could not open NBE file:";

    while (my $line = <FILE> ) {

      if ($line =~/results/) {

            our @values = split (/\|/, $line);

            print $values[3], "\n";
            
            }

        }
  
    }

}

Thanks radoulov and BubbaJoe, that helped alot. I had to go back and add a 'readline' function in line #19 to work properly. Thanks a bunch for your help!!!!!!!!!

use strict;
my @files=glob("*.nsb");
foreach my $key (@files){
        open FH,$key or die "Can not open $_";
        while(<FH>){
                my @tmp=split(",",$_);
                print $tmp[2] if ($_=~/pat/);
        }
        close FH;
}