Hi John, a few comments on your code (some of which was derived from the OP's).
On Mon, 01 Aug 2011 00:26:29 -0700 "John W. Krahn" <jwkr...@shaw.ca> wrote: > 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: $!"; > 1. You have used the string 'C:\Player' several times. Better put it in a variable: my $dir_name = 'C:\Player'; Then you can use it or interpolate it. 2. You can also use '/' on Windows instead of '\' which gives fewer problems when interpolating into qq{..} and other strings like that. 3. "FH" is generally short for "file-handle", but here it is a directory handle. 4. It should be a lexical variable: opendir my $dir_handle, $dir_name or die "Cannot open '$dir_name' because: $!"; > for my $file ( readdir FH ) This will slurp the entire directory contents into one big list and iterate over it. A somewhat less memory consuming version is: while (my $file = readdir($dir_handle)) which would read it in scalar context. Also see: File::Spec->no_upwards: http://perldoc.perl.org/File/Spec.html . > { > open DH, '<' $file or die "Cannot open '$file' because: $!"; 1. "DH" is short for "directory handle" and in this case it's a file handle. 2. It isn't lexical. 3. You're missing a comma between the «'<'» and the «$file». 4. This program will always throw an exception because it will try to open the directory entries "." and/or ".." for reading, which will fail because they are not files. > foreach my $line ( <DH> ) This will slurp the entire file into memory as a list/array of lines and then iterate over it. Better write it as: while (my $line = <$file_handle) > { > while ( $line =~ /"a=(\d),b=(\w+)"/gi ) > { > $sum += $2 - $1; > } $sum was not declared anywhere and the regular expression is a bit strange for what one dos with $1 and $2 later. Regards, Shlomi Fish > } > } > print "$sum\n"; > > __END__ > > > > John -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Stop Using MSIE - http://www.shlomifish.org/no-ie/ Chuck Norris read the entire English Wikipedia in 24 hours. Twice. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/