suggestions for tree data structure

F

freesoft12

I have a file containing many paths (files/links/dirs) and to remove
duplicate or redundant paths. I want to open the file and read each
path in that file and create a directory tree from it. Next, I want to
copy that directory tree extracted from the file and move it
underneath another directory (preserve the hierarchy).

For example, my file contains the following paths:

/a/b/c
/a/b
/a/b/d

In the first path, 'c' is a regular file. The second path can be
ignored since it is already covered by the first path. In the third
path, 'd' is a symbolic link that points to (say) ../e/f (a regular
file).

I want to move this directory tree underneath another directory, say /
tmp. Hence I will have /tmp/a/b/c, /tmp/a/b/d -> /tmp/a/e/f.

Can you suggest an efficient data structure to capture the directory
tree information?

Regards
Joe
 
T

Tim Greer

(e-mail address removed) wrote:

....
I want to move this directory tree underneath another directory, say /
tmp. Hence I will have /tmp/a/b/c, /tmp/a/b/d -> /tmp/a/e/f.

Can you suggest an efficient data structure to capture the directory
tree information?

Regards
Joe

What code do you have now for this task?
 
A

André Plöger

Hello,

I would use hash references for this task.

my %FileTree;
/a/b/c
/a/b
/a/b/d

perhaps something like this..
my $FileTree = {
'a' => {
'b' => {
'file' => 'c',
'link' => ''
}
},
'e' => {
'f' => {},
'g' => {
'file' => 'test'
}
}
};

Setting a link..
$FileTree->{'a'}{'b'}{'link'} = $FileTree->{'e'}{'g'};

my $File = $FileTree->{'a'}{'b'}{'link'}{'file'};

...now is the same..

my $File = $FileTree->{'e'}{'g'}{'file'};

This covers your path without redundancy and contains file and link
information.

Moving the tree..
my $FileTree2 = {
'tmp' => \%FileTree;
}

Best Regards,
André
 
X

xhoster

I have a file containing many paths (files/links/dirs) and to remove
duplicate or redundant paths.

I can't figure out what "and to remove duplicate or redundant paths"
means.
I want to open the file and read each
path in that file and create a directory tree from it. Next, I want to
copy that directory tree extracted from the file and move it

Which one is it, copy or move?
underneath another directory (preserve the hierarchy).

For example, my file contains the following paths:

/a/b/c
/a/b
/a/b/d

In the first path, 'c' is a regular file.

How do you know? Do you need to inspect the path/filename in Perl to
discover that, or do you know from some other part of the input file that
you haven't shown us?
The second path can be
ignored since it is already covered by the first path.

I'm not sure what that means. If /a/b/c implies /a/b, then why is /a/b in
the file in the first place? If /a/b is to be moved, what about /a/b/q,
which is not in your file but (perhaps) is in the file system?
In the third
path, 'd' is a symbolic link that points to (say) ../e/f (a regular
file).

Again, how do you know that? Does your file tell you that, or do you have
to inspect 'd' from within Perl to figure that out?
I want to move this directory tree underneath another directory, say /
tmp. Hence I will have /tmp/a/b/c, /tmp/a/b/d -> /tmp/a/e/f.

On my filesystem, symbolic links containing .. are not materialized. So d
being a symbolic link to ../e/f and d being a symbolic link to /a/e/f are
two different things that happen to refer to the same file. But if you
move d someplace else, they no longer would refer to the same file.


I can't figure out how what you are trying to do differs from:

mv /a /tmp

Can you suggest an efficient data structure to capture the directory
tree information?

I suspect some kind of hash-based structure will be as efficient as it
needs to be. Without knowing what is actually supposed to happen, it is
hard to say.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
D

David Combs

I have a file containing many paths (files/links/dirs) and to remove
duplicate or redundant paths. I want to open the file and read each
path in that file and create a directory tree from it. Next, I want to
copy that directory tree extracted from the file and move it
underneath another directory (preserve the hierarchy).

For example, my file contains the following paths:

/a/b/c
/a/b
/a/b/d

In the first path, 'c' is a regular file. The second path can be
ignored since it is already covered by the first path. In the third
path, 'd' is a symbolic link that points to (say) ../e/f (a regular
file).

I want to move this directory tree underneath another directory, say /
tmp. Hence I will have /tmp/a/b/c, /tmp/a/b/d -> /tmp/a/e/f.

Can you suggest an efficient data structure to capture the directory
tree information?

Regards
Joe

idea:

That standard depth-first-search algo for buildint a
topological-order tree -- do that, somehow.

Luckily, in the above example (and presumably the problem to
solve, generally), the paths all start at the same place.

When you get through you should have ONE tree that
encompasses all the paths.

Then you "simply" (dfs) read it out.

(Some work and also assembly required.)

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top