Sorting problem 
Author Message
 Sorting problem

I co-wrote a dyanmic directory listing program, but I can't seem to figure
out how to sort the results alphabetically... there's no array to sort.

I just need to figure out how to get the output of "\&found_file" to sort...
I'm not looking to rewriting the entire program. commented code would be
greatly appreciated too, as I am a bit of a Perl newbie. (=

Here's the related code:

use File::Find;

find(\&found_file, $target_dir);

sub found_file {
my $path = $File::Find::name;
my $file = $_; # $_ is the first argument passed to the func

# (constraints here) #

$file = $path; # now, copy $path to $file

my $file2 = $file; # $file is for the code, $file2 is for on-screen viewing

return if $file2 =~ m/.*\/.*/; # Skip files in subdirectories

# (more constraints here) #

if (-d $path) { # is a directory
print "<A HREF=\"/dirlist.cgi?dir=$dir$file2/\">$file2/</A>";

Quote:
} else {

print "<A HREF=\"$file\">$file2</A>";

Quote:
}

.... you get the idea.

Thanks,
  Adam Lyon



Fri, 16 Aug 2002 03:00:00 GMT  
 Sorting problem


Quote:
>I co-wrote a dyanmic directory listing program, but I can't seem to figure
>out how to sort the results alphabetically... there's no array to sort.

I had the same problem, and ended up customising File::Find.pm for that
purpose.  Instead of just one callback routine, it takes

and \&routine_to_call_when_finished_with_directory().  

http://www.inwap.com/mybin/File/Find.pm

    use File::Find;
    find3(\&wanted, \&dir_start, \&dir_end, '/foo','/bar');

    sub dir_end { ... }
    sub wanted { ... }

I suppose I ought to clean it up a little bit and submit it to CPAN.
        -Joe
--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.



Sat, 17 Aug 2002 03:00:00 GMT  
 Sorting problem

Quote:

>I co-wrote a dyanmic directory listing program, but I can't seem to figure
>out how to sort the results alphabetically... there's no array to sort.

>I just need to figure out how to get the output of "\&found_file" to sort...
>I'm not looking to rewriting the entire program. commented code would be
>greatly appreciated too, as I am a bit of a Perl newbie. (=

>Here's the related code:

>use File::Find;

>find(\&found_file, $target_dir);

>sub found_file {
>my $path = $File::Find::name;
>my $file = $_; # $_ is the first argument passed to the func

># (constraints here) #

>$file = $path; # now, copy $path to $file

>my $file2 = $file; # $file is for the code, $file2 is for on-screen viewing

>return if $file2 =~ m/.*\/.*/; # Skip files in subdirectories

># (more constraints here) #

>if (-d $path) { # is a directory
>print "<A HREF=\"/dirlist.cgi?dir=$dir$file2/\">$file2/</A>";
>} else {
>print "<A HREF=\"$file\">$file2</A>";
>}

I know your posting is 4 weeks old, but I'm a bit behind reading the group.
I'm not going to spoil your fun by giving you code, but here's a couple of
suggestions.

First, I note the line:

  return if $file2 =~ m/.*\/.*/; # Skip files in subdirectories

Does that mean you're not interested in recursing your directory structure,
or is there something else afoot? If you don't want to recurse, just use
opendir and readdir to read the contents of the directory you're interested
in. readdir in list context returns the whole directory in one command. So
(I lied when I said I wouldn't give you code) for directory name in
$directory:

opendir (MYDIR, $directory)  or  die "Couldn't open $directory: $!\n";


If you do need the recursion, the thing to do is to use &found_file to push
the details of the file it's found onto a global array, and then sort the
array afterwards.

Good luck.

+------------------------------+
|                              |
| David Chapman,               |
| Glastonbury, Somerset, UK.   |
|                              |

|                              |
+------------------------------+



Wed, 11 Sep 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Sort of a sort problem.

2. list sorting problem, please help

3. Sorting problem...

4. FormMail sort problem w/realname,email

5. sorting problem

6. Simple? sort problem

7. Funky sorting problem!!

8. sorting problem

9. Sorting problem -New to Perl

10. sorting problem

11. Sort Problem

12. sort problem

 

 
Powered by phpBB® Forum Software