problem about the perl code. thanks for any comments

Y

yezi

Hi all: the following is my program , Do not know why it can not
open file (*.RD ) in the current directory. Thanks for any comments.

Thanks


24 opendir DIR,".";
25 @files = readdir DIR;
26 foreach $file (@files)
27 {
28 if ($file =~ /\.RD/)
29 {
30 $sitename=chomp($file);
31 print "$sitename";
32
33 # print "Processing $_.. to table...";
34 open myRDfile, $sitename or die "Error:Could not
open $sitename ";
35 while($line=<myRDfile>){
36 chomp($line);
37 @parts = split(/ /,$line);
38 if ($parts[0] !~ /Entropy/)
39 { for ($j=-$DT;$j<=$DT;$j++)
40 { if($parts[0] =~ /RD[$j]/) {
$RD[($j+$DT)] = $parts[2];} } # mapping
41 }
42 else {$entropy =$parts[2];}
43 }
44 close myRDfile;
45 #output the RD metric to default
46 @part1= split(/\./,$sitename);
47 print "$part1[0]\t";
48 for($i=0;$i<=(2*$DT);$i++) {print "$RD[$i]\t";}
49 print"$entropy\n";
50
51 }
52 }
53
 
A

A. Sinan Unur

Hi all: the following is my program , Do not know why it can not
open file (*.RD ) in the current directory. Thanks for any comments.

Please post code that others can run by just copying and pasting. That
means no line numbers.

use strict;
use warnings;

missing.
24 opendir DIR,".";

Always, always check the return value of system calls

opendir my $current_dir, '.'
or die "Cannot open current directory: $!";
25 @files = readdir DIR;
26 foreach $file (@files)
27 {
28 if ($file =~ /\.RD/)
29 {

my @files = grep { /\.RD$/ } readdir $current_dir;
closedir $current_dir
or die "Cannot close current directory: $!";

for my $file (@files) {
30 $sitename=chomp($file);

1. Why are you chomping $file?
2. What do you thing chomp returns?

In all likelihood, $sitename is now set to 0.

Please read the posting guidelines for this group to lern how you can
help yourself, and help others help you.

Sinan
 
G

Gunnar Hjalmarsson

yezi said:
Do not know why it can not open file (*.RD ) in the current directory.

34 open myRDfile, $sitename or die "Error:Could not
open $sitename ";

In addition to Sinan's observations, if you want to know why an open()
statement fails, you should ask Perl about the reason.

open myRDfile, $sitename or die "Could not open $sitename: $!";
---------------------------------------------------------------^^
 
Y

yezi

Hi: Thanks for Sinan 's help. the following is the whole code. pls
check.


#!/usr/bin/perl -w


# DT value=$DT, site name=$site


my($DT,$site);
my($i,$j);
my($serial,$entropy);
my(@parts,@part1,@files);
my($file);
my($line);


$DT=25;
$site="download.microsoft.com";


for ($i=0;$i<=(2*$DT);$i++) {$RD[$i]=0;} #initiat the array of RD[]


#write the title of each column
print "Serial\t";
for($i=-$DT;$i<=$DT;$i++) {print "RD[$i]\t";}
print "Entrophy\n";


#analysis the RD file by serial
opendir DIR,"." or die "Cannot open current directory: $!";
@files = readdir DIR;
foreach $file (@files)
{
if ($file =~ /\.RD/)
{
# print "$file";


open myRDfile,$file or die "Error:Could not open $file ";
while($line=<myRDfile>){
chomp($line);
@parts = split(/ /,$line);
if ($parts[0] !~ /Entropy/)
{ for ($j=-$DT;$j<=$DT;$j++)
{ if($parts[0] =~ /RD[$j]/) { $RD[($j+$DT)] =
$parts[2];} } # mapping
}
else {$entropy =$parts[2];}
else {$entropy =$parts[2];}
}
close myRDfile;
#output the RD metric to default
@part1= split(/\./,$file);
print "$part1[0]\t";
for($i=0;$i<=(2*$DT);$i++) {print "$RD[$i]\t";}
print"$entropy\n";


}
}
 
B

Big and Blue

yezi said:
Hi all: the following is my program , Do not know why it can not
open file (*.RD ) in the current directory. Thanks for any comments.

As has been pointed out - checking and printing the results of opendir
and open will tell you that.

Other comments...
24 opendir DIR,".";
25 @files = readdir DIR;

You've forgotten the closedir().
26 foreach $file (@files)

Why not just loop on the readdir DIR, rather than slurping it into an
array?
27 {
28 if ($file =~ /\.RD/)
29 {
30 $sitename=chomp($file);

Why are you chomp()ing? You are reading entrynames from a directory -
if any of them ends with your $/ contents then it really does need them.
 
Y

yezi

the second version of code is posted on the above, the chomp is alredy
deleted . and closedir is added, but still have problem
 
J

John W. Krahn

yezi said:
Hi: Thanks for Sinan 's help. the following is the whole code. pls
check.

I would have but perl can't compile it:

$ perl -c problem_about_the_perl_code.pl
syntax error at problem_about_the_perl_code.pl line 47, near "else"
syntax error at problem_about_the_perl_code.pl line 57, near "}"
problem_about_the_perl_code.pl had compilation errors.


Please post code that perl can compile and please do not top-post. TIA



John
 
S

Scott Bryce

yezi said:
the second version of code is posted on the above, the chomp is
alredy deleted . and closedir is added, but still have problem

Your original problem was described as such:
Do not know why it can not open file (*.RD ) in the current
directory.

But you still haven't asked Perl why.
open myRDfile,$file or die "Error:Could not open $file ";

open myRDfile, $file or die "Error:Could not open $file -- $!";
-----------------------------------------------------------^^

This may give you the answer to your question. If not, post the error
message.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top