homedw wrote:
hi all,

Hello,


i want to open some files in a directory, you can see the details below,

#!/usr/bin/perl -w
use strict;
opendir (FH,'C:\Player');
chdir 'C:\Player';
for my $file(readdir FH)
{
         open DH,"$file";
         foreach my $line(<DH>)
         {
                 while($line=~/"a=(\d),b=(\w+)"/gi)
                 {
                 $sum+=$2-$1;
                 }
          }
}
print "$sum\n";


I can't open the files in 'C:\Player', can you help me find out where the
problem is?

Use error checking on your system calls and let the system tell you what the problem is:


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

opendir FH, 'C:\Player' or die "Cannot opendir 'C:\\Player' because: $!";
chdir 'C:\Player' or die "Cannot chdir to 'C:\\Player' because: $!";

for my $file ( readdir FH )
{
        open DH, '<' $file or die "Cannot open '$file' because: $!";
        foreach my $line ( <DH> )
        {
                while ( $line =~ /"a=(\d),b=(\w+)"/gi )
                {
                $sum += $2 - $1;
                }
         }
}
print "$sum\n";

__END__



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to