如何从目录树中在Perl中构建层次哈希

时间:2015-07-09 19:07:52

标签: perl recursion hash tree subroutine

我正在尝试构建这样的结构。

{
  "file1": "supersong.mp3",
  "file2": "supersong2.mp3",
  "file3": "text.txt",
  "file4": "tex2t.txt",
  "file5": "text3.txt",
  "file6": "json.pl",
  "directory_movies": [ 
      "file1": "supersong.mp3",
      "file2": "supersong2.mp3",
      "file3": "text.txt",
      "file4": "tex2t.txt",
      "file5": "text3.txt",
      "file6": "json.pl",
      "directory_sub_movies": [ 
           "file1": "supersong.mp3",
           "file2": "supersong2.mp3",
           "file3": "text.txt",
           "file4": "tex2t.txt",
           "file5": "text3.txt",
           "file6": "json.pl",
  ]

]     };

就像我在unix中的任何目录层次结构一样。所以我们有简单的文件或目录,如果它是目录,它是嵌套的哈希等,递归地 我需要在perl中将其表示为哈希,我发现最简单的方法是使用File::Find模块 它工作正常,但我无法弄清楚如何将哈希中的层次结构保存为如上所述嵌套 这是我的测试脚本。这正确地确定了当前项目的类型。

sub path_checker {
    if (-d $File::Find::name) {
        print "Directory " . $_ . "\n";   
    }
    elsif (-f $File::Find::name) {
        print "File " . $_ . " Category is " . basename($File::Find::dir) . "\n";  
    }

}
sub parse_tree {
    my ($class,$root_path) = @_;
    File::Find::find(\&path_checker, $root_path);
}

请帮助修改它以创建如上所述的结构。我会很感激。

1 个答案:

答案 0 :(得分:4)

子文件夹也应该是哈希,而不是数组,

use strict;
use warnings;

# use Data::Dumper;
use File::Find;
use JSON;

sub parse_tree {
    my ($root_path) = @_;

    my %root;
    my %dl;
    my %count;

    my $path_checker = sub {
      my $name = $File::Find::name;
      if (-d $name) {
        my $r = \%root;
        my $tmp = $name;
        $tmp =~ s|^\Q$root_path\E/?||;
        $r = $r->{$_} ||= {} for split m|/|, $tmp; #/
        $dl{$name} ||= $r;
      }
      elsif (-f $name) {
        my $dir = $File::Find::dir;
        my $key = "file". ++$count{ $dir };
        $dl{$dir}{$key} = $_;
      }
    };
    find($path_checker, $root_path);

    return \%root;
}

print encode_json(parse_tree("/tmp"));