【发布时间】:2016-09-26 16:33:07
【问题描述】:
use strict;
use warnings;
my %result_hash = ();
my %final_hash = ();
Compare_results();
foreach my $key (sort keys %result_hash ){
print "$key \n";
print "$result_hash{$key} \n";
}
sub Compare_results
{
while ( <DATA> )
{
my($instance,$values) = split /\:/, $_;
$result_hash{$instance} = $values;
}
}
__DATA__
1:7802315095\d\d,7802315098\d\d;7802025001\d\d,7802025002\d\d,7802025003\d\ d,7802025004\d\d,7802025005\d\d,7802025006\d\d,7802025007\d\d
2:7802315095\d\d,7802025002\d\d,7802025003\d\d,7802025004\d\d,7802025005\d\d,7802025006\d\d,7802025007\d\d
输出
1
7802315095\d\d,7802315098\d\d;7802025001\d\d,7802025002\d\d,7802025003\d\d,7802025004\d\d,7802025005\d\d,7802025006\d\d,7802025007\d\d
2
7802315095\d\d,7802025002\d\d,7802025003\d\d,7802025004\d\d,7802025005\d\d,7802025006\d\d,7802025007\d\d
我试图获取每个键的值并再次尝试从结果哈希中拆分逗号分隔值,如果我在任何值中找到分号,我希望将左右值存储在单独的哈希键中。
如下图
1.#split the value of result_hash{$key} again by , and see whether any chunk is seperated by ;
2. #every chunk without ; and value on left with ; should be stored in
@{$final_hash{"eto"}} = ['7802315095\d\d','7802315098\d\d','7802025002\d\d','7802025003\d\d','7802025004\d\d','7802025005\d\d','7802025006\d\d','7802025007\d\d'] ;
3.#Anything found on the right side of ; has to be stored in
@{$final_hash{"pro"}} = ['7802025001\d\d'] ;
有没有一种方法可以处理子程序中的所有事情?我可以让代码更简单吗
更新:
我尝试一次拆分字符串,但它只是用分号选择值并忽略所有内容
foreach my $key (sort keys %result_hash ){
# print "$key \n";
# print "$result_hash{$key} \n";
my ($o,$t) = split(/,|;/, $result_hash{$key});
print "Left : $o \n";
print "Left : $t \n";
#push @{$final_hash{"eto"}}, $o;
#push @{$final_hash{"pro"}} ,$t;
}
}
帮助后我更新的代码
sub Compare_results
{
open my $fh, '<', 'Data_File.txt' or die $!;
# split by colon and further split by , and ; if any (done in insert_array)
my %result_hash = map { chomp; split ':', $_ } <$fh> ;
foreach ( sort { $a <=> $b } (keys %result_hash) )
{
($_ < 21)
? insert_array($result_hash{$_}, "west")
: insert_array($result_hash{$_}, "east");
}
}
sub insert_array()
{
my ($val,$key) = @_;
foreach my $field (split ',', $val)
{
$field =~ s/^\s+|\s+$//g; # / turn off editor coloring
if ($field !~ /;/) {
push @{ $file_data{"pto"}{$key} }, $field ;
}
else {
my ($left, $right) = split ';', $field;
push @{$file_data{"pto"}{$key}}, $left if($left ne '') ;
push @{$file_data{"ero"}{$key}}, $right if($right ne '') ;
}
}
}
谢谢
【问题讨论】:
-
不错的想法,但是如果你在
/[,;]/上分裂,你如何判断哪些是“正确”的(;的右边)?它返回所有字段的列表,在,或;之间,无论它们发生的顺序如何。 (您编写的内容将列表分配给两 (2) 个标量,因此您只能在拆分后获得前两个元素。) -
我编辑了您的添加 - 将长评论移至其自己的行,因此不必滚动阅读它,并通过正则表达式添加了评论
# / ...,该评论关闭了编辑器错误的红色着色. (它经常这样做——在一个正则表达式之后,一切都变红了。)如果你不喜欢这个,请恢复更改——点击我的用户名上方的“已编辑(时间)”,你会看到修订,每个都有按钮。其中一个说“回滚”,按您要回滚到的修订点击它。 -
@zdim :您能告诉我如何拆分具有两个分隔符的数据,例如:1,2,3,4,5,6,7;8,9,10,11,12我想在之后存储所有内容;在一个数组中,然后在另一个数组中。请帮助我使用一次性拆分功能。 O/P array1 = [1,2,3,4,5,6,7] 和 array2 =[8,9,10,11,12]
-
目前,我的答案中最后一个示例的两次变体。第一个流行音乐 --
@left = split ",", (/([^;]+)/)[0]和@right = split ",", (/;([^;]+)/)[0];。这是如果要处理的字符串在$_中——如果它在变量中,则使用($str =~ /.../)[0]。加say for @left查看。(...)[0]是必需的,因为匹配返回一个列表,如果你将它传递给split,它在标量上下文中计算为1(长度)。真正的一次性更难,因为您会得到一份清单,但需要将其分成两份。如果我想出更好的,我会发表另一条评论。 -
但它是否会尝试将相同的数据拆分两次,这值得提出一个新问题吗?