stic wrote:
> Hello
Hello,
> this is first time i`m writing here so sorry for stupid questions or so.
> I need to make a program wich will list the directories and subdirectories.
> I find something like that here on this mailing list and i have changed
> it for my own needs.
> but now a have this problem:
> i want to print first the directories and its subdirectries and first
> after that a want to list normal files.
> so i have made some code for sorting the array and put it into the tree
> program.
> now it look like this
>
> #!/usr/bin/perl
You should also include the two pragmas:
use warnings;
use strict;
> use Term::ANSIColor qw(:constants);
> $cesta = $ARGV[0];
> sub do_dir {
> my $cesta = shift;
> opendir (OBSAH, $cesta);
You should *ALWAYS* verify that opendir worked correctly:
opendir OBSAH, $cesta or die "Cannot open '$cesta' $!";
> my @obsah = readdir(OBSAH);
> closedir(OBSAH);
> $t = $#obsah;
> for ($i= 0 ; $i < $t ; ++$i) {
> my $filename = $cesta . '/' . $obsah[$i];
> if ( $obsah[$i] eq '.' || $obsah[$i] eq '..' ) {
> delete ($obsah[$i]);
> } elsif (-d $filename) {
> push (@obsah2, $obsah[$i]);
> delete ($obsah[$i]);
> } else {
> push (@obsah3,$obsah[$i]);
> delete ($obsah[$i]);
> }
> }
> unshift(@obsahx,@obsah2);
> push(@obsahx,@obsah3);
> foreach my $prvok (@obsahx) {
> my $filename = $cesta . '/' . $prvok;
> next if ($prvok eq '.' || $prvok eq '..')
> elsif (-d $filename) {
You have a syntax error, that should be:
if (-d $filename) {
> ++$k;
> print "|------------" x $k;
> print GREEN, "$prvok\n", RESET;
> do_dir($filename);
> --$k;
> }
> else {
> ++$k;
> print "|------------" x $k;
> print BLUE, "$prvok\n", RESET;
> --$k;
> }
> }
> }
> do_dir($cesta);
>
>
> where $cesta is path which i write in shell and its path to directory
> wich i want to make a tree from.
> but this source code loops and never ends.
> can smbdy help me please?
It never ends because the arrays @obsahx, @obsah2 and @obsah3 are global and
you are only adding entries to them. You need to make them local to the
subroutine, something like:
my $k;
sub do_dir {
my $cesta = shift;
opendir my $OBSAH, $cesta or die "Cannot open '$cesta' $!";
my ( @obsah2, @obsah3 );
push @{ -d "$cesta/$_" ? [EMAIL PROTECTED] : [EMAIL PROTECTED] }, $_ for
grep !/\A\.\.?\z/,
readdir $OBSAH;
closedir $OBSAH;
for my $prvok ( @obsah2, @obsah3 ) {
my $filename = "$cesta/$prvok";
next if $prvok eq '.' || $prvok eq '..';
if ( -d $filename ) {
print "|------------" x ++$k, GREEN, "$prvok\n", RESET;
do_dir( $filename );
--$k;
}
else {
print "|------------" x ( $k + 1 ), BLUE, "$prvok\n", RESET;
}
}
}
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>