创建描述从给定目录获取的文件的哈希数组

时间:2017-12-08 01:06:39

标签: perl

我需要

  1. 从指定目录中读取文件列表

  2. 创建描述这些文件的哈希数组

  3. 我的程序从命令行获取目录的路径,打开它并读取其内容。将跳过目录和“点”文件,并打印每个其他条目的绝对路径。

    use strict;
    use warnings;
    
    use Data::Dumper qw(Dumper);
    use File::Spec;
    use Digest::SHA qw(sha256_hex);
    
    my $dir = $ARGV[0];
    
    opendir DIR, $dir or die "cannot open dir $dir: $!";
    
    while ( my $file = readdir DIR ) {
        next unless ( -f "${ \File::Spec->catfile($dir, $file) }" );
        next if ( "$file" =~ m/^\./) ;
        print "${ \File::Spec->rel2abs($file) }\n";
    }
    
    closedir DIR;
    

    在这里,我获取一个文件并使用路径,大小和sha256sum创建一个哈希。

    my $file = "file1.txt";
    
    my @fileref = (
        {
            path    =>  "Full path: " . File::Spec->rel2abs($file),
            size    =>  "Size (bytes): " . -s $file,
            id      =>  "SHA256SUM: " . sha256_hex($file),
        },
    );
    
    print "\n";
    print "$fileref[0]{path}\n";
    print "$fileref[0]{size}\n";
    print "$fileref[0]{id}\n";
    

    所有这些都有效,但我无法弄清楚如何迭代每个文件并将其添加到数组中。

    这就是我的计划

    for each file
        push file into array
        add the path, size, and id key:value pairs to file
    repeat
    

    如何生成必要的数组?

3 个答案:

答案 0 :(得分:2)

感谢Wumpus Q. Wumbley的push建议,我已经解决了我的问题:

my @array;
opendir DIR, $dir or die "cannot open dir $dir: $!";
while(my $file = readdir DIR) {
    next unless(-f "${\File::Spec->catfile($dir, $file)}");
    next if("$file" =~ m/^\./);
    #print "${\File::Spec->rel2abs($file)}\n";

    my  %hash = (
        path    =>  File::Spec->rel2abs($file),
        size    =>  -s $file,
        id      =>  sha256_hex($file),
    );

    push(@array, \%hash);

    #print Dumper sort \@array;
}
closedir DIR;

print Dumper \@array;

我创建"框架"对于散列,然后通过引用和push函数将其传递给数组。

答案 1 :(得分:2)

这是我的解决方案。我使用File::Spec::Functions而不是File::Spec保存了大量代码,只调用rel2abs一次

我还从哈希值中移除了标签,例如"Full path: "。没有理由在其中放置表示字符串:这是输出代码的作用

use strict;
use warnings;

use File::Spec::Functions qw/ catfile rel2abs /;
use Digest::SHA qw/ sha256_hex /;
use Data::Dumper qw/ Dumper /;

my ( $root ) = @ARGV;

$root = rel2abs( $root );    # Allow for relative path input

my @file_data;

{
    opendir my $dh, $root or die qq{Cannot open directory "$root": $!};

    while ( readdir $dh ) {

        next if /^\./;

        my $file = catfile( $root, $_ );

        next unless -f $file;

        push @file_data, {
            path => $file,
            size => -s $file,
            id   => sha256_hex( $file ),
        };
    }
}

print Dumper \@file_data;

答案 2 :(得分:0)

您拥有所有代码,您正在使用第一个元素创建一个数组。你的解除引用方式暗示了一种简单的方法来添加其他内容:

my $i = 0;
while(my $file = readdir DIR) {
        next unless(-f "${\File::Spec->catfile($dir, $file)}");
        next if("$file" =~ m/^\./);
        print "${\File::Spec->rel2abs($file)}\n";
        $fileref[$i]{path} = File::Spec->rel2abs($file);
        $fileref[$i]{size} = -s $fileref[0]{path};
        $fileref[$i]{id} = sha256_hex(fileref[0]{path});
        $i++;
}