in reply to Question about warnings and arrays

for opening all files in a directory consider perldoc -f readdir
my $zz = param('team'); my $z = param('squad'); if (param) { $sd = "../data/data_mlb"; opendir( DIR, $sd) || die; while( ($filename = readdir(DIR))){ next if "$sd\/$filename" =~ /\/\./; ##skip . files push @dots, "$sd\/$filename"; } ## end of while @dots = sort @dots; closedir(DIR); for(my $a=0;$a<@dots;$a++){ open (FILE, $dots[$a]); push @foo, <FILE>; if ($a+1 eq @dots) { close FILE; open (FILE, $dots[$a]); push @foo2, <FILE>; } ## end of if close FILE; } ## end of for

Replies are listed 'Best First'.
Re^2: Question about warnings and arrays
by Anonymous Monk on Aug 13, 2014 at 00:34 UTC

    Some suggestions and thoughts:

    1. Although I can't be certain, it looks like you're not using strict. Always use warnings; use strict;!!
    2. opendir( DIR, $sd) || die; would better with a lexical handle and the error message: opendir(my $dh, $sd) || die "couldn't opendir $sd: $!";
    3. Your opens would be better and safer with the three-argument form, lexical filehandles, and error handling: open (my $fh, '<', $dots[$a]) or die "couldn't open $dots[$a]: $!";
    4. The condition "$sd\/$filename" =~ /\/\./ will not just match (i.e. skip) .dotfiles, it'll skip any filenames that contain /. anywhere. For example, if $sd happens to contain that string someday, all files will be rejected. The condition $filename=~/^\./ would skip only filenames that begin with a dot and so that's probably better.
    5. I find the name @dots a little strange since it contains only files that don't begin with a dot?
    6. The last file of @dots is opened and read twice. An alternative would be to read <FILE> into a temporary array and store that into @foo, and into @foo2 when appropriate. Or you could move the special treatment of $dots[-1] outside of the loop (that would also have the stylistic advantage that the loop variable $a could be eliminated, e.g. for my $dot (@dots) { open (my $fh, '<', $dot) ...).
    7. I think the condition $a+1 eq @dots is more clearly written as $a==$#dots.
Re^2: Question about warnings and arrays
by Anonymous Monk on Aug 12, 2014 at 20:55 UTC

    While readdir might be an alternative to the OP's glob, what does that have to do with any of the OP's questions? Also that code is desperately crying for some code comments as to what it's doing. @foo and @foo2 seem to have nothing to do with OP's question, why add that confusing code?

      It's probably not as confusing as you think... Or I would not be getting any votes I'm guessing...
      Even if it doesn't help with this specific question, it could help someone else along the way... That's how I myself learn, so if it doesn't offend anyone who's "non-anonymous", I'll keep trying to help...
      What I'm trying to show is:
      read an entire directory of filenames into an array...
      skip files you know you don't want...
      Then read them back one at a time and do your thing (whatever that is)...
      That was valuable to me at one time...