【发布时间】:2011-10-04 06:39:26
【问题描述】:
结果:HASH(0x1948958) ARRAY(0x1978250) ./directory/filename 多行
想要的结果:[Key of first hash] [Key of second hash] ./directory/filename #(elements of array, currently working)
Catch:应该跨越到 N 级结构,因此我尝试使用 Data::Walk。
当我在结构中行走时,我真正想做的是引用正在使用的密钥。有点像 Data::Dumper 但制表符分隔而不是代码格式。我认为可能的解决方案(按优先顺序)是:
- 我忽略了一些对 Data::Walk 的调用。
- 我不知道这个任务的更好的模块。
- 我可以内联的快速代码 sn-p
- 我自己的模块/Data::Walk / Data::Dumper 的分支(皱眉)将添加此功能。
use strict;
use File::Basename;
use Data::Walk;
my $files;
while (<>) {
chomp;
#ls -l output in a file; referencing filename from it (8th column)
my @line = split(/ /, $_, 8);
#fileparse exported by File::Basename
my ($name,$path) = fileparse($line[7]);
open (my $fh, '<', $path . $name);
my $sha = Digest::SHA->new('sha1');
$sha->addfile($fh);
#finding files by basename, then unique hash, then however many places it is stored.
#question not why I don't use the hash as the first field.
#basename digest path
push(@{$files->{$name}->{$sha->hexdigest}}, $path . $name);
}
my @val;
sub walkit {
$val[$Data::Walk::depth - 1] = $_;
if ($Data::Walk::depth == 3) {
print join("\t", @val), "\n";
}
}
&walk (\&walkit, %$files);
大师?
【问题讨论】:
-
你想要什么输出?看起来你想要三列输出,但前两列你想要什么?
-
我要插入的键——基本名称、哈希、完整路径。我看到的不是键,而是由基本名称和哈希键重新定义的结构。
-
您好,我注意到您参与了“医疗保健 IT”堆栈交换,并认为您可能对此提案感兴趣 -> Healthcare Industry
标签: perl data-structures traversal