Mathew Snyder schreef:
> John W. Krahn:
>> Mathew:

>>> #!/usr/bin/perl -w
>>>
>>> use strict;
>>>
>>> my @filenames;
>>> my $processDir = "/usr/bin";
>>>
>>> opendir DH, $processDir or die "cannot open $processDir: $!";
>>> foreach my $file (sort(readdir DH)){
>>>         push @filenames, $file;
>>> }
>>
>> Why not just:
>>
>>   my @filenames = sort readdir DH;
>
> Being new to Perl I want to learn the "long" and traditional way
> before I begin figuring out all the shortcuts.

Don't see them as shortcuts. Read `perldoc perldsc`.
Learn how to handle single items (scalars, references too), and how to
handle groups (arrays, hashes).


> Having been overwhelmed by all of the numerous options and possible
> methods of accomplishing this I took what I understood from everyone's
> suggestions and came up with this:
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my $processDir = "/usr/bin";

No need for dquotes: there is nothing to interpolate.

Alternatives:

  my $processDir = '/usr/bin' ;

  my $processDir = q{/usr/bin} ;



> my @filenames;
> my $file_count = 0;
>
> opendir DH, $processDir or die "cannot open $processDir: $!";
> foreach my $file (readdir DH){
>         next if ($file =~  /^\.]$|^\.\.$/);
>         push @filenames, $file;
> }
> closedir DH;

The "]" should not be in the regex. Do you really only want to single
out '.' and '..' or also any other directories that might exist inside
$processDir?

The DH is old style, use the lexical "my $dh".

  { # new block, to limit the scope of $dh
    opendir my $dh, $processDir or die "open '$processDir': $!" ;
    @filenames = sort grep -f, readdir $dh ;
    closedir $dh or die "close '$processDir': $!" ;
  }


> foreach my $filename (sort(@filenames)) {
>         $filename = "$processDir/$filename";
>         my $mod_time = (stat($filename))[9];
>         print "$filename: $mod_time\n";
>         $file_count += 1;
> }
>
> print "\nThere are " . $file_count . " items in the filenames
> array.\n";

I liked your

  print "\nThere are " . scalar @filenames . " filenames.\n";

better.


> Dr. Ruud had mentioned that I was sorting too soon

Actually you weren't. But when you switched to a hash (which was a good
switch), you were.


> I was having a hard time
> understanding the - -f option and the line with grep in it so I
> didn't bother with those this time around.

See again `perldoc -f readdir`.



> Thanks everyone, for all you help.

I see still no proper sigsep here. Don't use PGP for technical mailing
lists and newsgroups: keep your messages as light and clean as humanly
possible.

> - --
> Mathew

-- 
Affijn, Ruud

"Gewoon is een tijger."



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to