Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^3: tree command

by RichardK (Parson)
on Oct 13, 2015 at 14:56 UTC ( #1144713=note: print w/replies, xml ) Need Help??

in reply to Re^2: tree command
in thread tree command

No, you can know the length of the list and still only process it once. The two things aren't related at all.

Replies are listed 'Best First'.
Re^4: tree command
by Skeeve (Parson) on Oct 14, 2015 at 06:21 UTC

    Okay. So would you mind telling me how I could filter out:

    • hidden files and directories (okay - "glob")
    • files

    without iterating over the content of the directory and know in advance that the last 3 items in my list don't count?

    Example: I have in my list [ a b c d e ] where b, d and e are files, which I do not want to display, while a and c are the directories to show. How would I know that in advance?

    With my approach I filter b, d and e out, leaving me with just [ a c ]. So I know that after "c" there won't be any more files or directories to be displayed.

    How would you achieve this?

      You can use
      my @dirs = glob '*/';

      To get the number of files, just subtract the number of dirs from the number of everything.

      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Which will iterate of the files a second time and will access the file system while I iterate over an array. Or am I wrong?


      Doesn't the code I posted do exactly that with only one pass? I didn't add the display file bit, but that's trivial to add if you need it.

      Here's the output of my code run on my junk directory, it seems to do what you ask :)

      +-dbf +-palindrome +-plot +-rules | +-abc1 | | +-ba | | +-de | | +-te

        if you didn't just post a part of the output, my tree would look like this:

        . \--dbf |--palindrome |--plot \--rules \--abc1 |--ba |--de \--te

        You see? I need to know, when I'm at "dbf", "rules", "abc1" and "te", that this one is the last element in the respective directory to stop printing the superfluous "|" characters you have.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2020-06-07 03:59 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (42 votes). Check out past polls.