gpt4 book ai didi

arrays - Perl - 散列的初始化

转载 作者:行者123 更新时间:2023-12-04 23:12:17 26 4
gpt4 key购买 nike

我不确定如何正确初始化我的散列 - 我正在尝试为输入文件中的耦合行中的值创建一个键/值对。

例如,我的输入是这样的:

@cluster t.18
46421 ../../../output###.txt/
@cluster t.34
41554 ../../../output###.txt/

我从第 1 行(@cluster 行)中提取 t 编号并将其与第二行中的 output###.txt 匹配(以 46421 开头的行)。但是,我似乎无法使用我编写的脚本将这些值放入我的哈希中。

#!/usr/bin/perl
use warnings;
use strict;

my $key;
my $value;
my %hash;

my $filename = 'input.txt';
open my $fh, '<', $filename or die "Can't open $filename: $!";

while (my $line = <$fh>) {
chomp $line;
if ($line =~ m/^\@cluster/) {
my @fields = split /(\d+)/, $line;
my $key = $fields[1];
}
elsif ($line =~ m/^(\d+)/) {
my @output = split /\//, $line;
my $value = $output[5];
}
$hash{$key} = $value;
}

最佳答案

这是个好主意,但是你的 $key 是用 my 创建的在 if block 中是一个作用域为该 block 的局部变量,屏蔽了全局 $key。在 if block 中,符号 $key 与您预先声明的符号无关。参见 my in perlsub .

一旦 if 完成,这个本地 $key 就会超出范围,并且不存在于 if block 之外。全局 $keyif 之后再次可用,在循环的其他地方可见,但未定义,因为它从未被分配给。 elsif block 中的 $value 也是如此。

只需将 my 声明放在循环中,从而分配给那些全局变量(如预期的那样?)。因此,$key = ...$value = ...,哈希将被正确分配。


注意——这是关于如何正确分配哈希值的。我不知道您的实际数据看起来如何以及该行是否被正确解析。这是一个玩具 input.txt

@cluster t.1 1111 ../../../output1.1.txt/@cluster t.2 2222 ../../../output2.2.txt/

I pick the 4th field instead of the 6th, $value = $output[3];, and add

print "$_ => $hash{$_}\n" for keys %hash;

循环之后。这打印

1 => output1.1.txt2 => output2.2.txt

I am not sure whether this is what you want but the hash is built fine.


A comment on choice of tools in parsing

You parse the lines for numbers, by using the property of split to return the separators as well, when they are captured. That is neat, but in some sense it reverses its main purpose, which is to extract other components from the string, as delimited by the pattern. Thus it may make the purpose of the code a little bit convoluted, and you also have to index very precisely to retrieve what you need.

Instead of using split to extract the delimiter itself, which is given by a regex, why not extract it by a regex? That makes the intention crystal clear, too. For example, with input

@cluster t.10 has 4319 elements, 0 subclusters 37652 ../../../../clust/output43888.txt 1.397428

the parsing can go as

if ($line =~ m/^\@cluster/) {
($key) = $line =~ /t\.(\d+)/;
}
elsif ($line =~ m/^(\d+)/) {
($value) = $line =~ m|.*/(\w+\.txt)|;
}
$hash{$key} = $value if defined $key and defined $value;

其中添加了 t\.\.txt 以更精确地指定目标。如果目标字符串不确定具有那种精确形式,只需捕获 \d+,在第二种情况下,最后一个 / 之后的所有非空格,例如 m|^\d+.*/(\S+)|。我们使用 .* 的贪心,它匹配 everything 到它后面的东西(a /),因此一直到最后一个 /

然后你也可以将它减少到每行一个正则表达式,例如

if ($line =~ m/^\@cluster\s+t\.(\d+)/) {
$key = $1;
}
elsif ($line =~ m|^\d+.*/(\w+\.txt)|) {
$value = $1;
}

请注意,我已将条件添加到哈希分配中。原始代码实际上在第一次迭代时分配了一个 undef,因为那时还没有看到 $value。这将在下一次迭代中被覆盖,如果我们之后只打印散列,我们将看不到它。该条件还可以防止匹配失败、格式错误的行等。当然,可以运行更好的检查。

关于arrays - Perl - 散列的初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37977057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com