Thread Ordnerstruktur in DB abbilden - evt. eigene DB (File) schreiben?
(34 answers)
Opened by lousek at 2011-02-24 00:10
Zeile 193 $tmphash = \%hash; schönes Beispiel, warum man mit globalen Variablen vorsichtig sein, und warum man "Sprechende" Variablennamen nutzen sollte. Hätte die %hash %global_hash_tree geheißen wäre es nie zu dem Fehler gekommen.
Außerdem: Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 for (my $i = 0; $i < @parts; $i++) { # walk through each part of the path my $part = $parts[$i]; if(defined($tmphash->{$part})) { # Does the part exists in the hash-tree? if($i == @parts-1) { # Is this the last part of the path? $found=1; # Yes, the folder is in the database! } else { # Not the last part of the path $tmphash=$tmphash->{$part}; # Go one level further } } else { last; # The path is not in the database } } lässt sich einfacher schreiben: Code (perl): (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 while(@parts) { # walk through each part of the path my $part=shift(@parts); my $elm=$tmphash->{$part}; # Does the part exists in the hash-tree? if(defined($elm)) { # Is this the last part of the path? if(!@parts) { # Yes, the folder is in the database! $found=1; #leave loop last; } # Not the last part of the path else { # if possible # Go one level further if(ref($elm) eq 'HASH') { $tmphash=$elm; } else { last; } } } else { # The path is not in the database last; } } |