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 '') ;
}
}
}
,451,515,
감사 말
에서
좋은 생각 (다시, 당신은 대신 직접
$data
을 복용%result_hash
을 구축 할 수 있습니다)하지만, 당신도'/ [,]에 분할하는 경우 /'어떻게 알 수 있습니까 어떤 것이 "올바른"것입니까 (';'의 오른쪽)? ','또는';'사이의 모든 필드 목록을 반환합니다. (당신이 썼던 것은 두개의 스칼라에리스트를 할당하기 때문에 처음 두 개의 원소 만 나눕니다.) – zdim추가 된 내용을 편집했습니다 - 긴 주석을 자체 라인으로 옮겨서 스크롤 할 필요가 없습니다 그리고 편집기에서 잘못된 빨강 색을 끄는 regex에 의한'#/...'주석을 추가했습니다. (자주 - 정규 표현식이 모두 붉은 색으로 변한 후에) 변경 사항을 되돌리려면 - 사용자 이름 위의 "편집 (시간)"을 클릭하면 각각의 버튼과 함께 수정본이 보일 것입니다. 그 중 하나가 "롤백"이라고 말하면 롤백하려는 버전을 클릭하십시오. – zdim
@zdim : 예를 들어 1,2,3,4,5,6,7,8,9,10,11,12의 두 구분 기호가있는 데이터를 분할하는 방법을 알려 주시겠습니까? 후에; 배열에서 그리고 다른 배열에서 바로. 원샷 분할 기능으로 나를 도와주세요. O/P array1 = [1,2,3,4,5,6,7] and array2 = [8,9,10,11,12] – LearningCpp