Note that the error indicated
"number of files: 4" isn't numeric
rather than
"4" isn't numeric
Subtracting 2 is not the way to go. Some directories on some file systems don't have "." and "..". It's better to just filter them out.
# Filter out "." and ".." my @array = grep !/^\.\.?\z/, readdir(DIR);
# Filter out all "hidden" files my @array = grep !/^\./, readdir(DIR);
There's no reason to use a global variable for your directory handle. Change
opendir (DIR, "/some/path"); my @array = grep !/^\.\.\z/, readdir(DIR); closedir (DIR);
to
opendir (my $dh, "/some/path"); my @array = grep !/^\.\.\z/, readdir($dh); closedir ($dh);
Finally, you should have some error checking, if only something minimalistic for opendir
opendir (my $dh, "/some/path") or die("opendir: $!\n");
In reply to Re: why scalar(@array) is not numeric?
by ikegami
in thread why scalar(@array) is not numeric?
by theiss
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |