I replaced everything after the second open with this:
my %parents_of;
my $parentid;
while ( my $line = <FFINFO> ) {
if ($line =~ /^Folder (\d{3,5})\s+\-\s+.*\./) {
$parentid = $1;
}
elsif ($line =~ /\s+subfolder\s+(\S+)\s+\-\s+.*\./) {
push @{ $parents_of{ $1 } }, $parentid;
}
}
sub build_path {
my $folderid = shift @_;
my @parents = @{ $parents_of{ $folderid } || [] };
return @parents ? [ map {
map { "$_/$folderid" } @{ build_path(
+$_ ) }
} @parents ]
: [ $folderid ];
}
foreach my $k (sort (keys (%folders))) {
$folderpaths{$k} = build_path($k);
print "$k => $_\n" for @{ $folderpaths{$k} };
}
The highlights of the changes are:
- I read the subfolders file only once.
- I store the subfolders data in a %parents_of hash of arrays, which maps each folder ID to the folders that have it as a subfolder.
- In build_path, I use the hash of arrays instead of iterating over the whole subfolders file. It's still recursive.
- Since build_path returns an array reference now, the output loop has to iterate its contents.
For the ID that you pointed out, the new output is:
3053 => 100/3051/3053
3053 => 100/3057/3053
3053 => 100/3063/3053
3053 => 100/3066/3053
The new code produces all the output of the old code plus another 1108 lines, and it runs in less than 1 second while the original takes about 220 seconds.
I hope this helps.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.