Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Array Normalization of File::DirWalk

by graff (Chancellor)
on Feb 23, 2007 at 08:16 UTC ( #601706=note: print w/replies, xml ) Need Help??

in reply to Array Normalization of File::DirWalk

Looking at the output you're getting, it seems like File::DirWalk is doing its traversal in such a way that the deeper paths are listed first, and assuming that will always be the case, then Enlil's suggestion should work fine.

There's another way, which would not be dependent on the assumption that the original listing will always be ordered like that, but it involves post-processing after the DirWalk thing is done:

@folder = sort @folder; my $npaths = scalar @folder; push @folder, "~~~"; @folder = grep /./, map { (index($folder[$_+1], "$folder[$_]\\") == 0) ? '' : $f +older[$_] } ( 0..$npaths );

That is, sort the array and eliminate an element if it is fully contained as an initial substring of the next element. (The standard string sort will always put "a1" before "a1\1".)

Note that I added a bogus element on the array, to avoid an "undefined value" warning when map reaches the last directory. It's not elegant, alas...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://601706]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-08-02 08:18 GMT
Find Nodes?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)

    Results (22 votes). Check out past polls.