in reply to Re: Building a UNIX path from Irritating data
in thread Building a UNIX path from Irritating data
And you can use the following two data files to make it work:#!/usr/bin/perl -w use strict; my $foldername_file = shift @ARGV; my $folderfolder_file = shift @ARGV; my %folders = (); my %folderpaths = (); open(FNAMES,"$foldername_file") or die "Can't open $foldername_file: $ +!\n"; foreach my $folderline (<FNAMES>) { chomp $folderline; if ($folderline =~ /^Folder (\d{3,5})\s+\-\s+(.*)\.$/) { $folders{$1} = $2; } } close(FNAMES); open(FFINFO,"$folderfolder_file") or die "Can't open $folderfolder_fil +e: $!\n"; my @subfolders = <FFINFO>; foreach my $k (sort (keys (%folders))) { $folderpaths{$k} = build_path($k,@subfolders); print "$k => $folderpaths{$k}\n"; } exit 0; sub build_path { my $folderid = shift @_; my @dumpff = @_; my $path = "$folderid"; my $parentid = ""; foreach my $line (@dumpff) { if ($line =~ /^Folder (\d{3,5})\s+\-\s+.*\./) { $parentid = $1; } elsif ($line =~ /\s+subfolder\s+$folderid\s+\-\s+.*\./ +) { $path = join('/', build_path($parentid,@subfol +ders),$folderid); } } return $path; }
foldernames.txt
subfolders.txt
Basically, the code above works fine for most all of the data, but there are about 60-70 folder ID's that appear as subfolders to more than one parent folder ID. Folder ID 3053 is a good example. It is a subfolder under 3051, 3057, 3063, and 3067. If you run my code, however, the path printed for 3053 is:
3053 => 100/3051/3053
To be totally complete, I would need to also print:
100/3057/3053
100/3063/3053
100/3067/3053
The only role build_dirs has is to find the parent folder ID of the folder ID it is given, and if the parent ID it finds isn't a root-level node, it calls itself again with the parent ID until a full path is created. However, since I'm building the path layer by layer, I can't figure out when to check for a duplicate path. Each iteration of build_dirs doesn't have any knowledge of any complete paths that have been found. I suppose I could return all the results from a search, but I'm not sure how that would look. Are you suggesting something like this?
3053 => 100/3051:3057:3063:3067/3053
Thank you for your assistance!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Building a UNIX path from Irritating data
by kyle (Abbot) on Nov 26, 2009 at 02:14 UTC | |
by roswell1329 (Acolyte) on Nov 29, 2009 at 23:22 UTC | |
by kyle (Abbot) on Nov 30, 2009 at 13:24 UTC | |
by roswell1329 (Acolyte) on Nov 30, 2009 at 23:09 UTC |