Perl Script Issue - Please Help * Thanks!!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Script Issue - Please Help * Thanks!!!
# 1  
Old 10-31-2008
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.
# 2  
Old 10-31-2008
All you need is:
Code:
perl -F'\|' -lane'
  print $F[2] if /results/
  ' *nbe

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

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

Or AWK, or ...
# 3  
Old 10-31-2008
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.
# 4  
Old 10-31-2008
Something like this:
Code:
#! /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
  }


Last edited by radoulov; 11-02-2008 at 06:54 PM..
# 5  
Old 10-31-2008
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. Smilie
# 6  
Old 11-01-2008
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.
# 7  
Old 11-01-2008
OK,
I understand, you only want to correct your own version. Try this:
Code:
#!/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";
            
            }

        }
  
    }

}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Perl : embedding java script with cgi perl script

Hi All, I am aware that html tags can be embedded in cgi script as below.. In the same way is it possible to embed the below javascript in perl cgi script ?? print("<form action="action.htm" method="post" onSubmit="return submitForm(this.Submitbutton)">"); print("<input type = "text"... (1 Reply)
Discussion started by: scriptscript
1 Replies

3. Shell Programming and Scripting

Issue regarding dos2unix perl script

Hi All, I have pearl script which will check and convert the file: INFO("dos2unix_cmds".$#{$dos2unix_cmds}); if ( $#{$dos2unix_cmds} == 0 ) { my $convert_cmd = $$dos2unix_cmds; my $rename_cmd = $$dos2unix_cmds; --conversion going here else INFO ("No need to... (8 Replies)
Discussion started by: saps19
8 Replies

4. Shell Programming and Scripting

Perl script issue

Hi All, I have a perl script which I am using in Windows environment. There is one more file called "functions.txt" which is having all the functions defined to used in my perl script. And the path for this function file is defined in my perl script. Howeever sometimes I am getting below error... (4 Replies)
Discussion started by: gr8_usk
4 Replies

5. Web Development

Accessing a Perl CGI script, security issue

Hi Everybody, I was wondering if it was possible for someone to gain access to my Perl CGI scripts before they are interpreted by Perl (mod_perl on apache2) i.e. getting a hold of my raw scripts and not the html output? Let's say I use the DBI module where I have the hostname, user and... (2 Replies)
Discussion started by: z1dane
2 Replies

6. Shell Programming and Scripting

Perl script issue: print

Can someone tell me what I'm doing wrong with my perl script? I am new to Perl. This isn't even the final script, I'm just testing to see if it will print the directories with the files in it. For some reason my output is only printing the 1st, 6th, and last entries of the @sub_dir array. Each... (3 Replies)
Discussion started by: man
3 Replies

7. Shell Programming and Scripting

Perl issue - please help!

Hello. I've been writing some code in Perl to read in strings from html files and have been having issues. In the html file, each "paragraph" is a certain file on the website. I need to find every one of the files that is a certain type, in this case, having green color....therefore... (7 Replies)
Discussion started by: akreibich07
7 Replies

8. Shell Programming and Scripting

Perl Script issue. What am I doing wrong?

#!/usr/local/bin/perl open (MYFILE, 'logs_report'); while (<MYFILE>) { $rec=$_; chomp ($rec); @arr=split(/ /,$rec); print $rec,"\n" if ($arr!~/OK/); open (MYF, '>data.txt'); print $rec,"\n" if ($arr!~/OK/); close (MYF); (14 Replies)
Discussion started by: SkySmart
14 Replies

9. Shell Programming and Scripting

Perl Issue

Hi, I got this script from the web, this generates an LDAP report in CSV format. #!/usr/bin/perl # # Copyright (c) 2004 # Ali Onur Cinar &060;cinar&064;zdo.com&062; # # License: # # Permission to use, copy, modify, and distribute this software and its # documentation for... (23 Replies)
Discussion started by: raj001
23 Replies

10. Shell Programming and Scripting

perl issue ..

hi one perl issue i have xml file with 2 values and one condition b.w them <rule> <val1>12</val1> <cond>and</cond> <val2>13</val2> </rule> i read these values in hash in perl code $one{val1} = 12 $one{cond} = and $one{val2} = 13 now i want to form... (3 Replies)
Discussion started by: zedex
3 Replies
Login or Register to Ask a Question