作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想读取文件的内容并将其保存在数组的散列中。每行的第一列将是关键。然后,我要读取一个目录下的文件,根据key把文件名添加到数组末尾!
文件($file_info)
AANB John male
S00V Sara female
SBBA Anna female
目录中的文件:
AANB.txt
SBBA.txt
S00V.txt
预期输出:
AANB John male AANB.txt
S00V Sara female S00V.txt
SBBA Anna female SBBA.txt
这是脚本本身:
#!/usr/bin/perl
use strict;
use warnings;
my %all_samples=();
my $file_info = $ARGV[0];
open(FH, "<$file_info");
while(<FH>) {
chomp;
my @line = split("\t| ", $_);
push(@{$all_samples{$line[0]}}, $_);
}
my $dir = ".";
opendir(DIR, $dir);
my @files = grep(/\.txt$/,readdir(DIR));
closedir(DIR);
foreach my $file (@files) {
foreach my $k (keys %all_samples){
foreach my $element (@{ $all_samples{$k} }){
my @element = split(' ', $element);
if ($file =~ m/$element[0]/) {
push @{$all_samples{$element}}, $file;
}
else {
next;
}
}
}
}
foreach my $k (keys %all_samples) {
foreach my $element (@{ $all_samples{$k} }) {
print $element,"\n";
}
}
但是输出不是我所期望的
AANB John male
SBBA.txt1
S00V Sara female
SBBA Anna female
S00V.txt1
AANB.txt1
最佳答案
我认为
my @element = split(' ', $element);
if ($file =~ m/$element[0]/) {
push @{$all_samples{$element}}, $file;
}
没有做正确的事情,所以 $all_samples{$element}}
是一个新的 arrayref。您正在打印六个单元素数组,而不是三个二元素数组。
但是每行打印一个数组元素也无济于事。
我认为你的最后一部分应该看起来更像这样:
foreach my $k (keys %all_samples) {
print join( "\t", @{ $all_samples{$k} } ) . "\n"
}
总的来说,我认为您使这个脚本过于复杂了。我会这样写:
#!/usr/bin/perl
use strict;
use warnings;
my $all_samples={};
while(<>) {
chomp;
# Note that I'm using variable names here to document
# The format of the file being read. This makes for
# easier trouble-shooting -- if a column is missing,
# It's easier to tell that $file_base_name shouldn't be
# 'Anna' than that $line[0] should not be 'Anna'.
my ( $file_base_name, $given_name, $sex ) = split("\t", $_);
push(@{$all_samples->{$file_base_name} }, ( $file_base_name, $given_name, $sex ) );
}
my $dir = ".";
opendir(DIR, $dir);
my @files = grep(/\.txt$/,readdir(DIR));
closedir(DIR);
FILE: foreach my $file (@files) {
BASE: foreach my $base (keys %{$all_samples}){
next BASE unless( $file =~ /$base/ );
push @{$all_samples->{$base}}, $file;
}
}
foreach my $k (keys %{$all_samples} ) {
print join( "\t", @{ $all_samples->{$k} } ) . "\n";
}
我更喜欢 hashrefs 而不是 hashes,仅仅是因为我倾向于处理嵌套数据结构——我只是更习惯于看到 $all_samples->{$k}
而不是 $all_samples {$k}
...更重要的是,我正在使用 arrayref 的全部功能,这意味着我不必重新拆分已经拆分过一次的数组。
G. Cito 提出了一个有趣的观点:我为什么使用
push(@{$all_samples->{$file_base_name} }, ( $file_base_name, $given_name, $sex ) );
而不是
push(@{$all_samples->{$file_base_name} }, [ $file_base_name, $given_name, $sex ] );
后者在语法上没有任何错误,但这不是我想要完成的:
让我们看看 $all_samples->{$base} 之后会是什么样子
push @{$all_samples->{$base}}, $file;
如果原来的推送是这样的:
push(@{$all_samples->{$file_base_name} }, [ $file_base_name, $given_name, $sex ] );
@{$all_samples->{$base}}
看起来像这样:
(
[ $file_base_name, $given_name, $sex ],
$file
)
如果相反,我们使用
push(@{$all_samples->{$file_base_name} }, ( $file_base_name, $given_name, $sex ) );
@{$all_samples->{$base}}
在 push @{$all_samples->{$base}}, $file
之后看起来像这样:
(
$file_base_name,
$given_name,
$sex,
$file
)
例如:
(
"AANB",
"John",
"male",
"AANB.txt"
)
所以当我们打印数组时:
print join( "\t", @{ $all_samples->{$k} } ) . "\n";
将打印
AANB John male AANB.txt
关于perl - 向数组散列中的数组添加新元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28776993/
我是一名优秀的程序员,十分优秀!