Re: Recognize directories and files.
From: Paul Lalli (mritty_at_gmail.com)
Date: 12/09/04
- Next message: Paul Lalli: "Re: passing string to ksh with system command"
- Previous message: joez3_at_yahoo.com: "passing string to ksh with system command"
- In reply to: Mitchell Hulscher: "Re: Recognize directories and files."
- Next in thread: Mitchell Hulscher: "Re: Recognize directories and files."
- Reply: Mitchell Hulscher: "Re: Recognize directories and files."
- Reply: John W. Krahn: "Re: Recognize directories and files."
- Reply: Anno Siegel: "Re: Recognize directories and files."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Paul Lalli: "Re: passing string to ksh with system command"
- Previous message: joez3_at_yahoo.com: "passing string to ksh with system command"
- In reply to: Mitchell Hulscher: "Re: Recognize directories and files."
- Next in thread: Mitchell Hulscher: "Re: Recognize directories and files."
- Reply: Mitchell Hulscher: "Re: Recognize directories and files."
- Reply: John W. Krahn: "Re: Recognize directories and files."
- Reply: Anno Siegel: "Re: Recognize directories and files."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|