Re: only reading every other directory
- From: Todd de Gruyl <todd@xxxxxxxxxxxx>
- Date: Sun, 25 Sep 2005 00:11:18 -0500
On 2005-09-25, Derek Lamb <derek@xxxxxxxxxxxxxxxx> wrote:
> use strict;
> use warnings;
> my @dirs=<Good/*>; #get the directories
> my ($i,$datfile);
You might want to limit your variable scopes to where they are used:
> print "@dirs\n"; #print the directories
> for $i(0..scalar(@dirs)-1){
this could be:
for my $i (0..scalar(@dirs)-1){
or (better)
for my $i (0..@#dirs){
> print $i+1,"\t$dirs[$i]\t"; #print some sanity checks
> $datfile=<$dirs[$i]/*>; #get the name of the dat file
similarly:
my ($datfile) = <$dirs[$i]/*>;
(the parentheses make sure we are in list context, assigning the first
result of the glob operation to $datfile and discarding any other
values)
from perldoc perlop:
A (file)glob evaluates its (embedded) argument only when it is starting
a new list. All values must be read before it will start over. In
list context, this isn't important because you automatically get them
all anyway. However, in scalar context the operator returns the next
value each time it's called, or "undef" when the list has run out. As
with filehandle reads, an automatic "defined" is generated when the
glob occurs in the test part of a "while", because legal glob returns
(e.g. a file called 0) would otherwise terminate the loop. Again,
"undef" is returned only once. So if you're expecting a single value
from a glob, it is much better to say
($file) = <blurch*>;
than
$file = <blurch*>;
The undef is probably the problem.
> print "$datfile\n"; # and print it
> }
>
so, to sumarize, with:
#!/usr/bin/perl
use strict;
use warnings;
my @dirs=<Good/*>; #get the directories
print "@dirs\n"; #print the directories
for my $i(0..scalar(@dirs)-1){
print $i+1,"\t$dirs[$i]\t"; #print some sanity checks
my ($datfile)=<$dirs[$i]/*>; #get the name of the dat file
print "$datfile\n"; # and print it
}
it works for me.
--
Todd de Gruyl
.
- References:
- only reading every other directory
- From: Derek Lamb
- only reading every other directory
- Prev by Date: Re: only reading every other directory
- Next by Date: Camel book relevance
- Previous by thread: Re: only reading every other directory
- Next by thread: Camel book relevance
- Index(es):
Relevant Pages
|