公告

<2024年8月>
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

统计

  • 随笔 - 9
  • 文章 - 13
  • 评论 - 3
  • 引用 - 0

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

Perl--------树形浏览文件夹

上次用perl的数组的可操作性用队列的方式遍历了文件夹,昨天写了一个建树的函数,利用两个数组,最后将结果保存在一个数组中(感想:c++绝对是世界上最好的语言,一个永远都不会失控的语言)

# !c:/perl/bin/perl.exe -w

our   @build_dirs ;
our   @temp1_dirs ;
our   @temp2_dirs ;

$dir   =   " downloads " ;
push ( @temp1_dirs ,   $dir );
# +++++++++++++++++++++++++++++++#
sub  EnumDirs{
    
@dir_f   =   @_ ;
    
my   @temp_dirs ;
    
my   $path   =   join ( //,   @dir_f );     # 数组转化为标量?

    
$ret_b   =   opendir (HFILE ,   $path );
    
die   " open dir error $! "   if ( ! $ret_b );
    
@file_s   =   readdir (HFILE);

    
for ( $count   =   0 $count < @file_s $count ++ )
    {
        
        
if ( ! $file_s [ $count =~  m /\./  )               # 打印文件;
        {
            
unshift ( @temp2_dirs ,   $file_s [ $count ]);        # if has dirs shift the path into @temp2_dirs
             return   0 ;                                                 # return 0;
        }
        
return   1 ;                                                        # if none dirs return 1;
    }
    
}
# ++++++++++++++++++++++++++++++#
sub  CreateTree{
    
my   @dir_fs   =   @_ ;

    
$path   =   shift ( @temp1_dirs );
    
die   " none elems! "   if ( $path   ==  NULL);
    
push ( @build_dirs   , $path );     # 没有文件夹写入最终的队列;

LOOP
: {
        
$i   =   & EnumDirs( $path );
}

    
if ( 1   ==   $i )
    {
        
if ( @temp2_dirs   ==  NULL)
        {
            
Print ();
        }
        
    }
    
if ( 0   ==   $i )
    {
        
$path   =   shift ( @temp2_dirs );
         
unshift ( @temp1_dirs ,   $path );
         
next  LOOP;
    }
    
}

posted on 2006-09-15 17:19 blues 阅读(342) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理