这是代码,我知道它不是完美的 perl。如果您对我如何做得更好有见识,请告诉我。我的主要问题是如何在不使用 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";
}
}