Home > Archive > PERL Miscellaneous > August 2005 > problem about the perl code. thanks for any comments
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
problem about the perl code. thanks for any comments
|
|
|
| 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. Sinan Unur 2005-08-30, 6:58 pm |
| "yezi" <ye_line@hotmail.com> wrote in news:1125434263.574194.118650
@z14g2000cwz.googlegroups.com:
> 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
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| Gunnar Hjalmarsson 2005-08-30, 6:58 pm |
| yezi wrote:
> Do not know why it can not open file (*.RD ) in the current directory.
<snip>
> 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: $!";
---------------------------------------------------------------^^
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
|
| 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";
}
}
A. Sinan Unur wrote:
> "yezi" <ye_line@hotmail.com> wrote in news:1125434263.574194.118650
> @z14g2000cwz.googlegroups.com:
>
>
> Please post code that others can run by just copying and pasting. That
> means no line numbers.
>
> use strict;
> use warnings;
>
> missing.
>
>
> Always, always check the return value of system calls
>
> opendir my $current_dir, '.'
> or die "Cannot open current directory: $!";
>
>
> my @files = grep { /\.RD$/ } readdir $current_dir;
> closedir $current_dir
> or die "Cannot close current directory: $!";
>
> for my $file (@files) {
>
>
> 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
>
> --
> A. Sinan Unur <1usa@llenroc.ude.invalid>
> (reverse each component and remove .invalid for email address)
>
> comp.lang.perl.misc guidelines on the WWW:
> http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| Big and Blue 2005-08-30, 6:58 pm |
| yezi wrote:
>
> 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.
--
Just because I've written it doesn't mean that
either you or I have to believe it.
| |
|
| the second version of code is posted on the above, the chomp is alredy
deleted . and closedir is added, but still have problem
| |
| John W. Krahn 2005-08-30, 9:56 pm |
| yezi wrote:
> 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
--
use Perl;
program
fulfillment
| |
| Scott Bryce 2005-08-30, 9:56 pm |
| yezi wrote:
> 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.
|
|
|
|
|