Derek Lamb wrote: > A very peculiar (to me) behavior has been observed when I run a simple > script, > and I wanted to get some other eyes looking at it. I have a directory called > "Good", and subdirectories "01", "02", ... "05". In each directory is a > single file, "01.dat", "02.dat" etc. All I want to do (for now) is to find > and print the name of the dat files. Simple, yes? So I do this: > > use strict; > use warnings; > my @dirs=<Good/*>; #get the directories > my ($i,$datfile); > print "@dirs\n"; #print the directories > for $i(0..scalar(@dirs)-1){ > print $i+1,"\t$dirs[$i]\t"; #print some sanity checks > $datfile=<$dirs[$i]/*>; #get the name of the dat file > print "$datfile\n"; # and print it > } > > and I get the following results: > $ perl test.pl > Good/01 Good/02 Good/03 Good/04 Good/05 > 1 Good/01 Good/01/01.dat > Use of uninitialized value in concatenation (.) or string at test.pl line 9. > 2 Good/02 > 3 Good/03 Good/03/03.dat > Use of uninitialized value in concatenation (.) or string at test.pl line 9. > 4 Good/04 > 5 Good/05 Good/05/05.dat > > The result is that it found all the directories, but it only finds the dat > file in directories 1,3, and 5, not 2 and 4. The warning doesn't really tell > me anything that I didn't already know. To make sure I am not crazy, I do: > > $ ls Good/0?/*.dat > Good/01/01.dat Good/02/02.dat Good/03/03.dat Good/04/04.dat Good/05/05.dat > > Any ideas?
perldoc perlop [snip] 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*>; because the latter will alternate between returning a filename and returning false. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>