简体   繁体   English

perl打印带有Data :: Dumper的数组的哈希值

[英]perl printing hash of arrays with out Data::Dumper

Here is the code, I know it is not perfect perl. 这是代码,我知道它不是完美的perl。 If you have insight on how I an do better let me know. 如果您对我如何更好地了解我有所了解。 My main question is how would I print out the arrays without using Data::Dumper? 我的主要问题是如何在不使用Data :: Dumper的情况下打印出数组?

#!/usr/bin/perl
use Data::Dumper qw(Dumper);

use strict;
use warnings;

open(MYFILE, "<", "move_headers.txt") or die "ERROR: $!";

#First split the list of files and the headers apart
my @files;
my @headers;
my @file_list = <MYFILE>;
foreach my $source_parts (@file_list) {
  chomp($source_parts);
  my @parts = split(/:/, $source_parts);
  unshift(@files, $parts[0]);
  unshift(@headers, $parts[1]);
}

# Next get a list of unique headers
my @unique_files;
foreach my $item (@files) {
  my $found = 0;
  foreach my $i (@unique_files) {
      if ($i eq $item) {
        $found = 1;
        last;
      }
  }
  if (!$found) {
    unshift @unique_files, $item;
  }
}
@unique_files = sort(@unique_files);

# Now collect the headers is a list per file
my %hash_table;
for (my $i = 0; $i < @files; $i++) {
  unshift @{ $hash_table{"$files[$i]"} }, "$headers[$i]";
}

# Process the list with regex
while ((my $key, my $value) = each %hash_table) {
  if (ref($value) eq "ARRAY") {
    print "$value", "\n";
  }
}

The Perl documentation has a tutorial on " Printing of a HASH OF ARRAYS " (without using Data::Dumper ) Perl文档有一个关于“ 打印HASH OF ARRAYS ”的教程(不使用Data::Dumper

perldoc perldsc perldoc perldsc

An alternative to using Data::Dumper is to use Data::Printer: 使用Data :: Dumper的另一种方法是使用Data :: Printer:

use Data::Printer;
p $value;

You can also use this to customise the format of the output. 您也可以使用它来自定义输出的格式。 Eg you can have it all in a single line without the indexes (see the documentation for more options): 例如,您可以在没有索引的情况下将它们全部放在一行中(有关更多选项,请参阅文档 ):

use Data::Printer {
    index     => 0,
    multiline => 0,
};
p $value;

Also, as a suggestion for getting unique files, put the elements into aa hash: 另外,作为获取唯一文件的建议,将元素放入哈希:

my %unique;
@unique{ @files } = @files;
my @unique_files = sort keys %unique;

Actually, you could even skip that step and put everything into %hash_table in one pass: 实际上,您甚至可以跳过该步骤,并在一次传递中将所有内容放入%hash_table:

my %hash_table;
foreach my $source_parts (@file_list) {
    chomp($source_parts);
    my @parts = split(/:/, $source_parts);
    unshift @{ $hash_table{$parts[0]} }, $parts[1];  
}

You're doing a couple things the hard way. 你正在做一些艰难的事情。 First, a hash will already uniqify its keys, so you don't need the loop that does that. 首先,哈希已经将其密钥唯一化,因此您不需要执行该操作的循环。 It appears that you're building a hash of files, with the values meant to be the headers found in those files. 您似乎正在构建文件哈希,其值应该是在这些文件中找到的标头。 The input data is "filename:header", one per line. 输入数据是“filename:header”,每行一个。 (You could use a hash of hashes, since the headers may need uniquifying, but let's let that go for now.) (你可以使用散列哈希,因为标题可能需要统一,但让我们现在就去做。)

use strict;
use warnings;
open my $files_and_headers, "<", "move_headers.txt" or die "Can't open move_headers: $!\n";

my %headers_for_file;
while (defined(my $line = <$files_and_headers> )) {
        chomp $line;
        my($file, $header) = split /:/, $line, 2;
        push @{ $headers_for_file{$file} }, $header;
}

# Print the arrays for each file:
foreach my $file (keys %headers_for_file) {
    print "$file: @{ $headers_for_file{$file}}\n";
}

We're letting Perl do a chunk of the work here: 我们让Perl在这里完成一大部分工作:

  1. If we add keys to a hash, they're always unique. 如果我们向哈希添加键,它们总是唯一的。
  2. If we interpolate an array into a print statement, Perl adds spaces between them. 如果我们将数组插入到print语句中,Perl会在它们之间添加空格。
  3. If we push onto an empty hash element, Perl automatically puts an empty anonymous array in the element and then pushes onto that. 如果我们推送一个空的哈希元素,Perl会自动在元素中放入一个空的匿名数组,然后推送到该元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM