简体   繁体   English

如何在Perl中将字符串转换为哈希表

[英]How to convert string to hash table in perl

I have a string from a for loop: 我有一个来自for循环的字符串:

@file = "/path/window/*_testing_42.csv";


foreach $file(@file) {


$name = $file=~ /(\w*)_testing_42/; #comes from file path
$name = 1$;
print $name; #prints G43B76P90T45

}

There are 4 values I am I need from this string (G43, B76, P90, T45). 我需要从此字符串中获取4个值(G43,B76,P90,T45)。 I want to place these into a hash so that I can refer to each value specifically. 我想将它们放入一个散列中,以便可以专门引用每个值。 However the hash table code I am trying to implement is not working for my intended purpose: 但是,我要实现的哈希表代码无法满足我的预期目的:

 my %hash;



foreach $file(@file) {


    $name = $file=~ /(\w*)_testing_42/; #comes from file path
    $name = 1$;
    print $name; #prints G43B76P90T45



    my($first $second $third $fourth) = $name;
    $hash{"first"} = $first;
    $hash{"second"} = $second;
    $hash{"third"} = $third;
    $hash{"fourth"} = $fourth;

EXPECTED OUTPUT: 预期的输出:

    print $fourth; #should print T45


    print $first; #should print G43
    print $third #should print  P90
}

First you need to split the name in 4 parts: 首先,您需要将名称分为4部分:

my ($first, $second, $third, $fourth) = unpack("(A3)*", $name);

Fill the hash 填写哈希

$hash{"first"} = $first;
$hash{"second"} = $second;
$hash{"third"} = $third;
$hash{"fourth"} = $fourth;

and print the hash 并打印哈希

print $hash{"fourth"};

If I understand correctly what you're trying to do, then @Gever's answer should do the trick. 如果我正确理解了您要做什么,那么@Gever的答案应该可以解决问题。 Here's an alternative implementation using regexes rather than unpack: 这是使用正则表达式而不是解压缩的替代实现:

use 5.010;
use strict;
use warnings;

my @file = glob("/path/window/*_testing_42.csv");

foreach my $file (@file) {
    my($name) = $file =~ /(\w+)_testing_42/;
    my @code = $name =~ /(...)/g;
    say 'Parts found: ', scalar(@code);   # Parts found: 4
    say $code[0];   # G43
    say $code[1];   # B76
    say $code[2];   # P90
    say $code[3];   # T45
}

I used an array rather than a hash, because that makes more sense to me, but if you really want a hash, you could do it like this: 我使用数组而不是哈希,因为这对我来说更有意义,但是如果您确实想要哈希,则可以这样做:

foreach my $file (@file) {
    my($name) = $file =~ /(\w+)_testing_42/;
    my %hash;
    @hash{'first', 'second', 'third', 'fourth'} = $name =~ /(...)/g;
    say $hash{first};   # G43
    say $hash{second};  # B76
    say $hash{third};   # P90
    say $hash{fourth};  # T45
}

In this line: 在这一行:

my($name) = $file =~ /(\w+)_testing_42/;

The parentheses around $name are important because they force the match to be evaluated in list context, which returns the parts of the regex that were captured in the (\\w+) . $name左右的括号很重要,因为它们会强制在列表上下文中评估匹配项,这会返回在(\\w+)中捕获的正则表达式部分。 Without the parentheses, the value 1 would be assigned to $name because there was 1 match. 如果没有括号,则将值1分配给$name因为存在1个匹配项。

The syntax for assigning a list of values to a series of keys in a hash (called a 'hash slice') is somewhat confusing. 在哈希(称为“哈希切片”)中为一系列键分配值列表的语法有些令人困惑。 Perl knows we're assigning values into %hash because of the { after the variable name, but we put a @ before the variable name to indicate we're assigning multiple values to a hash slice. Perl的知道我们正在分配值到%hash ,因为的{后的变量名,但我们把@变量名前,表示我们对散列切片分配多个值。 Using a $ before the variable name would indicate we're assigning to a single value in the hash. 在变量名之前使用$表示我们正在将哈希值分配给单个值。

The other thing I changed from your code is that I declared %hash inside the loop. 我从您的代码中更改的另一件事是,我在循环内声明了%hash Which means that you can only refer to it inside the loop. 这意味着您只能在循环内引用它。 If you declare it outside the loop, one set of values will persist after each matching filename has been processed, but the hash might contain values from different filenames depending on how many fields were present on the last iterations. 如果在循环外声明它,则一组值将在处理完每个匹配的文件名后保留,但是散列可能包含来自不同文件名的值,具体取决于上一次迭代中存在多少个字段。

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

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