open files in a directory
open files in a directory
am 01.08.2011 09:09:34 von homedw
--00148539273637e7c904a96c5016
Content-Type: text/plain; charset=ISO-8859-1
hi all,
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()
{
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?
thanks,
Jack
--00148539273637e7c904a96c5016--
Re: open files in a directory
am 01.08.2011 09:26:29 von jwkrahn
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()
> {
> 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 ( )
{
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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: open files in a directory
am 01.08.2011 09:34:20 von Emeka
--000e0cd247b0cb45de04a96ca8a7
Content-Type: text/plain; charset=ISO-8859-1
I guess that $sum should be "my $sum" before using it.
Emeka
On Mon, Aug 1, 2011 at 8:26 AM, John W. Krahn wrote:
> homedw wrote:
>
>> hi all,
>>
>
> Hello,
>
>
>
> i want to open some tha 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()
>> {
>> 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 ( )
> {
> 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-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
--
*Satajanus Nig. Ltd
*
--000e0cd247b0cb45de04a96ca8a7--
Re: open files in a directory
am 01.08.2011 10:29:43 von timothy adigun
--0016e6d7ee9be212ff04a96d6ef2
Content-Type: text/plain; charset=ISO-8859-1
Hi Jack,
Your code should work fine except for d following:
1. your $sum used in the while loop is out of scope
i.e declare it before your first for loop, and intialize to zero. As u
have it in your code.
The last print statement will print nothing 'cos $sum is out of reach, so u
see nonthing!
2. with the "use strict" used you shld ve error message telling about global
symbol "$sum" required explicit package name. So, u shld also do my $sum.
3. You might also need to check your inner while loop.
a.) \d ==> means digits, while \w+ ==> means one or more
characters(words), so I wonder why you are trying to perform arithematic on
dem! [Pls correct me if am wrong! about what are trying to do],
b.) You don't ve to put $line is a quote to match!
Note: Apart from what I stated above, your code shld
work, cos I ve tried ur code to print out a large file in another drive and
it worked perfectly, after the correction stated above was used!
Regards
On Mon, Aug 1, 2011 at 8:09 AM, homedw wrote:
> hi all,
>
> 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()
> {
> 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?
>
> thanks,
> Jack
>
--0016e6d7ee9be212ff04a96d6ef2--
Re: open files in a directory
am 01.08.2011 10:30:16 von timothy adigun
--00163646db70d5301e04a96d7034
Content-Type: text/plain; charset=ISO-8859-1
Hi Jack,
Your code should work fine except for d following:
1. your $sum used in the while loop is out of scope
i.e declare it before your first for loop, and intialize to zero. As u
have it in your code.
The last print statement will print nothing 'cos $sum is out of reach, so u
see nonthing!
2. with the "use strict" used you shld ve error message telling about global
symbol "$sum" required explicit package name. So, u shld also do my $sum.
3. You might also need to check your inner while loop.
a.) \d ==> means digits, while \w+ ==> means one or more
characters(words), so I wonder why you are trying to perform arithematic on
dem! [Pls correct me if am wrong! about what are trying to do],
b.) You don't ve to put $line is a quote to match!
Note: Apart from what I stated above, your code shld
work, cos I ve tried ur code to print out a large file in another drive and
it worked perfectly, after the correction stated above was used!
Regards
On Mon, Aug 1, 2011 at 8:34 AM, Emeka wrote:
> I guess that $sum should be "my $sum" before using it.
>
> Emeka
>
> On Mon, Aug 1, 2011 at 8:26 AM, John W. Krahn wrote:
>
> > homedw wrote:
> >
> >> hi all,
> >>
> >
> > Hello,
> >
> >
> >
> > i want to open some tha 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()
> >> {
> >> 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 ( )
> > {
> > 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-unsubscribe@perl.org
> > For additional commands, e-mail: beginners-help@perl.org
> > http://learn.perl.org/
> >
> >
> >
>
>
> --
> *Satajanus Nig. Ltd
>
>
> *
>
--00163646db70d5301e04a96d7034--
Re: open files in a directory
am 01.08.2011 10:50:41 von Shlomi Fish
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" wrote:
> homedw wrote:
> > hi all,
>=20
> Hello,
>=20
>=20
> > 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()
> > {
> > while($line=3D~/"a=3D(\d),b=3D(\w+)"/gi)
> > {
> > $sum+=3D$2-$1;
> > }
> > }
> > }
> > print "$sum\n";
> >
> >
> > I can't open the files in 'C:\Player', can you help me find out where t=
he
> > problem is?
>=20
> Use error checking on your system calls and let the system tell you what=
=20
> the problem is:
>=20
>=20
> #!/usr/bin/perl
> use warnings;
> use strict;
>=20
> opendir FH, 'C:\Player' or die "Cannot opendir 'C:\\Player' because: $!";
> chdir 'C:\Player' or die "Cannot chdir to 'C:\\Player' because: $!";
>=20
1. You have used the string 'C:\Player' several times. Better put it in a
variable:
my $dir_name =3D '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 ha=
ndle.
4. It should be a lexical variable:
opendir my $dir_handle, $dir_name=20
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 =3D 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.=20
> foreach my $line ( )
This will slurp the entire file into memory as a list/array of lines and th=
en
iterate over it. Better write it as:
while (my $line =3D <$file_handle)
> {
> while ( $line =3D~ /"a=3D(\d),b=3D(\w+)"/gi )
> {
> $sum +=3D $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";
>=20
> __END__
>=20
>=20
>=20
> John
--=20
------------------------------------------------------------ -----
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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/