Thread RDW #1 - Rätsel der Woche Nummer eins (112 answers)
Opened by Crian at 2004-07-08 21:52

Gast Gast
 2004-07-12 18:38
#84014 #84014
[quote=Ronnie,11.07.2004, 23:41]
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

print Dumper walk('./rdw01_testsuit');
exit;

sub walk {
 my $here = {};
 chdir shift;  
 $here->{$_} = (-d $_) ? walk($_) : '' for (<*>);
 chdir '..';
 return $here;
}

[/quote]
Diese Lösung gefällt mir am Besten ...
kurz und trocken :)

Hab' da noch etwas dran rumgebastelt.
Wenn man das so schreibt:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/perl
#-#############################################
   
   use strict;
   use warnings;
   use Data::Dumper;

   my $root_dir = 'AbsoluterPfadZumVerzeichnis';
   
   print "Content-type: text/plain\n\n";
   print Dumper get_tree(\$root_dir);  
   
   print "\n\nRuntime: ", ((times())[0] + (times())[1]), " Sec.";

#-#############################################
# Sub: Create a Tree of Directories and Files
#-#############################################
sub get_tree {
   my ($root, $dir) = @_;
   my ($path, $tree);
   local $_;
   
   $$root =~ s/\\/\//g;
   
   $dir ? ($path = "$$dir/$$root") : ($path = $$root);
   
   chdir $$root;
   $tree->{$_} = -d($_) ? get_tree(\$_, \$path) : $path foreach <*>;
   chdir '..';
   
   return $tree;
}

#-#############################################


dann wird das Teil auch ein wenig schneller und zu jeder Datei findet sich der entsprechende Pfad im Value des Keys.
Dachte das könnte vielleicht für die eine oder die andere Anwendung irgendwie nützlich sein.
Antworten mit Zitat

View full thread RDW #1 - Rätsel der Woche Nummer eins