Thread Wie nach komplettem Dateinamen alphabetisch sortieren (11 answers)
Opened by GwenDragon at 2023-01-07 18:05

rosti
 2023-01-08 15:51
#194582 #194582
User since
2011-03-19
3492 Artikel
BenutzerIn
[Homepage]
user image
Try that:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# fetch directories
my @dirs = do{
local $/ = undef;
split /\n/, <DATA>;
};

# mischen
fisher_yates_shuffle( \@dirs );

# sortieren
@dirs = sort{ weight($a) <=> weight($b) } @dirs;

while( my $e = shift @dirs ){
print "$e\n";
}

# perlfaq4
sub fisher_yates_shuffle {
my $deck = shift; # $deck is a reference to an array
return unless @$deck; # must not be empty!

my $i = @$deck;
while (--$i) {
my $j = int rand ($i+1);
@$deck[$i,$j] = @$deck[$j,$i];
}
}

# bewerten
sub weight{
my $path = shift;
my @p = split /\//, $path;
my $score = 0;
for( my $i = 0; $i < scalar @p; $i++){
$score += ord($p[$i]) * 2 ** $i;
}
return $score;
}


__DATA__
/
/abc/
/abc/test/
/abc/test/z.php4
/abc/test/z.txt
/asdf/
/asdf/123/
/asdf/A/
/asdf/B/
/asdf/B/C/
/asdf/B/C/a.file
/home/
/home/test/
/home/test/asdf/
/home/test/asdf/jklo/
/home/test/asdf/jklo/rstuv2/
/home/test/asdf/jklo/rstuvw11/
/root/config
/root/tmp/

View full thread Wie nach komplettem Dateinamen alphabetisch sortieren