Arrays Perl拆分并丢弃一行中的第一个元素
我有一些数据,应该能够很容易地分割成散列 下面的代码旨在将字符串拆分为相应的键/值对,并将输出存储在散列中 代码:Arrays Perl拆分并丢弃一行中的第一个元素,arrays,perl,hash,split,Arrays,Perl,Hash,Split,我有一些数据,应该能够很容易地分割成散列 下面的代码旨在将字符串拆分为相应的键/值对,并将输出存储在散列中 代码: use Data::Dumper; # create a test string my $string = "thing1:data1thing2:data2thing3:data3"; # Doesn't split properly into a hash my %hash = split m{(thing.):}, $string; print Dumper(\%hash
use Data::Dumper;
# create a test string
my $string = "thing1:data1thing2:data2thing3:data3";
# Doesn't split properly into a hash
my %hash = split m{(thing.):}, $string;
print Dumper(\%hash);
$VAR1 = {
'data3' => undef,
'' => 'thing1',
'data2' => 'thing3',
'data1' => 'thing2'
};
# There is an extra blank element at the start of the array
my @data = split m{(thing.):}, $string;
for my $line (@data) {
print "LINE: $line\n";
}
LINE:
LINE: thing1
LINE: data1
LINE: thing2
LINE: data2
LINE: thing3
LINE: data3
但是,在检查输出时,很明显,此代码没有按预期工作
输出:
use Data::Dumper;
# create a test string
my $string = "thing1:data1thing2:data2thing3:data3";
# Doesn't split properly into a hash
my %hash = split m{(thing.):}, $string;
print Dumper(\%hash);
$VAR1 = {
'data3' => undef,
'' => 'thing1',
'data2' => 'thing3',
'data1' => 'thing2'
};
# There is an extra blank element at the start of the array
my @data = split m{(thing.):}, $string;
for my $line (@data) {
print "LINE: $line\n";
}
LINE:
LINE: thing1
LINE: data1
LINE: thing2
LINE: data2
LINE: thing3
LINE: data3
为了进一步研究这个问题,我将输出分割成一个数组,然后打印结果 代码:
use Data::Dumper;
# create a test string
my $string = "thing1:data1thing2:data2thing3:data3";
# Doesn't split properly into a hash
my %hash = split m{(thing.):}, $string;
print Dumper(\%hash);
$VAR1 = {
'data3' => undef,
'' => 'thing1',
'data2' => 'thing3',
'data1' => 'thing2'
};
# There is an extra blank element at the start of the array
my @data = split m{(thing.):}, $string;
for my $line (@data) {
print "LINE: $line\n";
}
LINE:
LINE: thing1
LINE: data1
LINE: thing2
LINE: data2
LINE: thing3
LINE: data3
输出:
use Data::Dumper;
# create a test string
my $string = "thing1:data1thing2:data2thing3:data3";
# Doesn't split properly into a hash
my %hash = split m{(thing.):}, $string;
print Dumper(\%hash);
$VAR1 = {
'data3' => undef,
'' => 'thing1',
'data2' => 'thing3',
'data1' => 'thing2'
};
# There is an extra blank element at the start of the array
my @data = split m{(thing.):}, $string;
for my $line (@data) {
print "LINE: $line\n";
}
LINE:
LINE: thing1
LINE: data1
LINE: thing2
LINE: data2
LINE: thing3
LINE: data3
正如您所看到的,问题在于split
在数组的开头返回了一个额外的空元素
是否有任何方法可以从拆分输出中丢弃第一个元素,并将其存储在一行的散列中?
我知道我可以将输出存储在数组中,然后将第一个值移开,将数组存储在散列中。。。但我只是好奇这是否可以一步完成将丢弃第一个值。
my(未定义,%hash)=拆分m{(thing.):},$string
将丢弃第一个值。我建议使用regex
而不是split
:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my $string = "thing1:data1thing2:data2thing3:data3";
my %results = $string =~ m/(thing\d+):([A-Z]+\d+)/ig;
print Dumper \%results;
当然,这确实假设您正在匹配“单词+数字”组,因为如果没有“数字”分隔符,它将无法正常工作。
我的主要目的是演示这种技术——从字符串中获取“成对”值,因为它们直接分配给散列
使用正则表达式可能需要稍微复杂一点,例如非reedy量词:
my %results = $string =~ m/(thing.):(\w+?)(?=thing|$)/ig;
这可能会降低它的清晰度 我建议-使用
regex
而不是split
:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my $string = "thing1:data1thing2:data2thing3:data3";
my %results = $string =~ m/(thing\d+):([A-Z]+\d+)/ig;
print Dumper \%results;
当然,这确实假设您正在匹配“单词+数字”组,因为如果没有“数字”分隔符,它将无法正常工作。
我的主要目的是演示这种技术——从字符串中获取“成对”值,因为它们直接分配给散列
使用正则表达式可能需要稍微复杂一点,例如非reedy量词:
my %results = $string =~ m/(thing.):(\w+?)(?=thing|$)/ig;
这可能会降低它的清晰度 不。除非数据非常可预测,以至于每个键看起来都像
data\d
,否则这是一个荒谬的解决方案。对于“有没有办法从拆分输出中丢弃第一个元素?”这个问题,我认为这是正确的答案。如果数据是如此可预测,那么最好只编写my%hash=(thing1=>'data1',thing2=>'data2',thing3=>'data3')
放在第一位否。除非数据非常可预测,以至于每个键看起来都像data\d
这是一个荒谬的解决方案,因为“有没有任何方法可以从拆分输出中丢弃第一个元素?”我认为这是正确的答案。如果数据是如此可预测,那么最好只写my%hash=(thing1=>'data1',thing2=>'data2',thing3=>'data3')
首先,对于一个好的答案+1,但对于我正在尝试做的事情来说,它要复杂得多,而且split比regex保持清晰得多。是的,它并不总是适用。但对于许多情况,它确实适用,因为你可以放弃记录分隔符、换行符等。对于一个好的答案+1,但对于我正在尝试做的事情它要复杂得多,而且split比正则表达式更能保持清晰。是的,它并不总是适用。但在许多情况下,它确实适用,只是因为你可以丢弃记录分隔符、换行符等。