【问题标题】:How to assign two arrays to a hash and correlate them by name basis in Perl如何将两个数组分配给一个哈希并在 Perl 中按名称关联它们
【发布时间】:2013-06-20 03:26:09
【问题描述】:

我有两个数组,都包含一个文件名列表。除了扩展名之外,两个数组中的文件名都是相同的。

filename.dwgfilename.zip

现在,我已将每个文件列表分配给一个数组。

@dwg_files@zip_files

最终,我要做的是检查不同数组中两个同名文件之间的最后修改日期,如果一个文件比另一个文件年轻,则运行一个脚本。到目前为止,我所拥有的似乎有效,除非它比较两个具有不同名称的文件。我需要它将第一个数组中的文件与另一个数组中的相同文件进行比较。

asdf1.dwg 应该与 asdf1.zip

相关联
my $counter = 0 ;
while ( $counter < @dwg_files ) {
    print "$counter\n";
    my $dwg_file = $dwg_files[$counter];
    my $zip_file = $zip_files[$counter];


#check if zip exists
if (-e $zip_file) {

     #Checks last modification date
     if (-M $dwg_file < $zip_file) {
         *runs script to creat zip*

     } else { 
         *Print "Does not need update."*
     }

} else {
    *runs script to create zip*
}

$counter++;
}

做了一些研究,我想我会尝试使用哈希来关联两个数组。我似乎无法弄清楚如何通过名称关联它们。

my %hash;
@hash{@dwg_files} = @zip_files;

我是一个完整的 Perl 菜鸟(上周才开始使用它)。我已经坚持了好几天了,任何帮助都会非常感激!

【问题讨论】:

    标签: arrays perl hash


    【解决方案1】:

    您可以取 dwg 文件名,将扩展名更改为 zip,然后继续检查,

    for my $dwg_file (@dwg_files) {
    
        my $zip_file = $dwg_file;
        print "dwg:$dwg_file\n";
        $zip_file =~ s/[.]dwg/.zip/i or next;
    
    
      #check if zip exists
      if (-e $zip_file) {
    
           #Checks last modification date
           if (-M $dwg_file < -M $zip_file) {
               #*runs script to creat zip*
    
           } else { 
               #*Print "Does not need update."*
           }
    
      } else {
          #*runs script to create zip*
      }
    
    }
    

    【讨论】:

    • 打印 dwg 文件以检查哪个被跳过。
    • 更好的是,将for my $counter (0 .. $#dwg_files)替换为for my $dwg_file (@dwg_files)并跳过下面$dwg_file的初始化。
    • @mzedeler OP 打印 $counter 所以看起来它是需要的
    • $counter 用于问题中的while 循环控制,因此不一定需要(打印它似乎是为了调试)
    • @doubleDown 现在没有计数器了。
    【解决方案2】:

    要将所有文件名存储在哈希中,您可以执行以下操作:

    #!/usr/bin/perl
    use Data::Dumper;
    
    # grab all dwg and zip files
    my @dwg_files = glob("*.dwg");
    my @zip_files = glob("*.zip");
    
    sub hashify {
       my ($dwg_files, $zip_files) = @_;
       my %hash;
    
       # iterate through one of the arrays
       for my $dwg_file ( @$dwg_files ) {
            # parse filename out
            my ($name) = $dwg_file =~ /(.*)\.dwg/;
    
            # store an entry in the hash for both the zip
            # and dwg files
            # Entries of the form:
            # { "asdf1" => ["asdf1.dwg", "asdf1.zip"]
            $hash{$name} = ["$name.dwg", "$name.zip"];
       }
    
       # return a reference to your hash
       return \%hash;
    }
    
    # \ creates a reference to the arrays 
    print Dumper ( hashify( \@dwg_files, \@zip_files ) );
    

    这是生成的哈希的样子:

    {
      'asdf3' => [
                   'asdf3.dwg',
                   'asdf3.zip'
                 ],
      'asdf5' => [
                   'asdf5.dwg',
                   'asdf5.zip'
                 ],
      'asdf2' => [
                   'asdf2.dwg',
                   'asdf2.zip'
                 ],
      'asdf4' => [
                   'asdf4.dwg',
                   'asdf4.zip'
                 ],
      'asdf1' => [
                   'asdf1.dwg',
                   'asdf1.zip'
                 ]
    };
    

    【讨论】:

    • @zip_files 发生了什么?它正在创建但未使用。
    • @mzedeler 他们可以使用任何一个数组,我将两者都传入来说明这一点。我想也可能存在这样一种情况,即您遍历两个列表并将存在的 zip 文件添加到哈希中。
    • 您好,感谢您的帮助。我有一个简单的问题:当你使用我的 @/dwg_files = glob(".dwg");我的@/zip_files = glob(".zip");您根据同一目录中的文件填充数组。您将如何使用来自不同目录(在本例中为子文件夹)的文件填充数组。
    • 在glob函数中指定目录即可,print glob("$base_directory/*.zip");
    猜你喜欢
    • 2011-04-12
    • 2011-07-20
    • 1970-01-01
    • 2010-11-22
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    相关资源
    最近更新 更多