Re: Recognize directories and files.

From: Paul Lalli (mritty_at_gmail.com)
Date: 12/09/04


Date: Thu, 09 Dec 2004 19:41:06 GMT


"Mitchell Hulscher" <unacceptable@gmail.com> wrote in message
news:41b8a84e$0$1257$ba620dc5@nova.planet.nl...
> I just took a look at the replies to my post, and I was glad
> there were people that could help me to find an answer.
> I must say I found the first reply kind of "rough"

Have you read the posting guidelines for this group yet? You're far
less likely to receive rough replies when you follow those guidelines.

>, but I DID
> manage to get my code working. So, thanks a lot.

You're welcome.

> I was wrong thinking that looking for a period would be my
> solution just because I couldn't figure out the -d and -f
> switches.

Yes.

> Anyway, this is my new code:
>
>
> #!C:\perl\bin\
>

you're still missing
use strict;
> use warnings;
>
> print "Enter a full directory path to examine: ";
>
> chomp($dir = <STDIN>);
>
> $dir .= "\\" unless $dir =~ /\\+$/;
>
> opendir(HANDLE, $dir) || die "Unable to open directory for
> analysis: $!";
>
> foreach (readdir(HANDLE)) {
> $path = $dir . $_;
> if (-d $path) {
> push(@dirs, $path);
> } elsif (-f $path) {
> push(@files, $path);
> }

All well and good, but I'd probably rewrite it like so:

push (-d $path ? @dirs : @files), $path));

> }
>
> sort(@files);
> sort(@dirs);

Once again, what do you think this is doing? @files and @dirs remain
completely unchanged by this operation. sort() returns the sorted list,
it does not modify a given list.

> print "\n\nDirectories:\n------------\n";
>
> foreach (@dirs) {
> print "$_\n";
> }
>
> print "\n\nFiles:\n------\n";
>
> foreach (@files) {
> print "$_\n";
> }
>
> #End of program.
>
> I'm glad I got it working, and if anyone still has any comments
> on this code, please let me know.
> What is annoying, is that the arrays are empty at program load,
> and Perl returns an error saying it cannot sort empty void.

No it doesn't. First, it returns a warning, not an error. Second, that
warning reads:
Useless use of sort in void context

This has nothing at all to do with arrays being empty. Try adding
use diagnostics;
to the top of your code to have perl explain why it's giving you this
warning. (I have already told you why, of course).

> Ofcourse, the arrays will be filled later, but I was wondering
> if there was any way of preventing this "error".

The arrays are already non-empty by the time you call sort. You've
misdiagnosed the problem.

Paul Lalli



Relevant Pages

  • RE: Offer a feature to disable the sort warning in Excel 2003
    ... And not just the sort warning for adjacent data found, ... > This is just a suggestion to MS. Feel free to add your opinion if you wish. ... > This post is a suggestion for Microsoft, ...
    (microsoft.public.excel.misc)
  • Re: OT - How to get away with murder
    ... Giving a warning to a suicide bomber isn't the smartest thing to do. ... some sort of detonation system which would explode if shot, a sort of, ... There was no "Stop Police" and quite frankly, with all the palavra, ...
    (uk.media.tv.misc)
  • Re: Cyber Bullying In UMTM - The Die Is Cast
    ... Is that why there are so many replies to this ... What can be observed is that there is a culture in UMTM of allowing ... UMTM's apparent addiction to CB-bashing. ... those not involved so show some sort of acknowledgement. ...
    (uk.media.tv.misc)
  • Re: OT - How to get away with murder
    ... Giving a warning to a suicide bomber isn't the smartest thing to do. ... some sort of detonation system which would explode if shot, a sort of, ... I hadn't being able to locate the mains switch and the neighbour had ...
    (uk.media.tv.misc)
  • Re: Microsoft and Trust Take 2
    ... think everyone did a stellar job of avoiding answer the question. ... replies and no answer must be some sort of record :-) So here's the ...
    (microsoft.public.vb.general.discussion)