이 방법으로 해시와 배열을 조작 한 것은 이번이 처음입니다. 기본적으로 모든 키에 대해 여러 값을 기록한 다음 "키 -> 값 -> 값 -> 값 ..."형식으로 인쇄합니다.배열을 사용하는 Perl 해시를 빌드하는 올바른 방법입니까?
내 코드는 다음과 같습니다. 나는 그것이 효과가 있다는 것에 놀랐으며, "실수로"효과가 있다고 우려했다. 이 작업을 수행하는 올바른 방법입니까 아니면 더 효율적이거나 적절한 방법이 있습니까?
while ($source =~ m/(regex)/g) { #Get all key names from source
$listkey = $1; #Set current list key to the current regex result.
$list{$listkey} = ++$i unless $list{$listkey}; #Add the key to the hash unless it already exists.
$list{$listkey} = [] unless exists $list{$listkey}; #Add an array for the hash unless the hash already exists.
while ($loopcount==0) {
if ($ifcount==0) {
$listvalue=result_of_some_function_using_list_key; #Get the first list value by using the list key.
$ifcount++; #Increment so we only get the first list value once.
} else {
$listvalue=result_of_some_function_using_list_value; #Update the list value by using the last list value.
}
if ($listvalue) { #If the function returned a value...
push @{$list{$listkey}}, $listvalue; #...then add the value to the hash array for the key.
} else { #There are no more values and we need a new key.
$listkey=0; #Reset variable.
$listvalue=0; #Reset variable.
$loopcount++; #Increment loop counter to exit loop.
}
}
$ifcount=0; #Reset count variable so the next listvalue can be generated from the new key.
$loopcount=0; #Reset count variable so another loop can begin for a new key.
}
foreach $listkey (keys %list) { #For each key in the hash.
print "$listkey --> "; #Print the key.
@values = @{$list{$listkey}}; #Reference the arrays of the hash.
print join ' --> ', @values; #Print the values.
print "\n"; #Print new line.
}
정말로 'use strict; 경고 사용; mode – Zaid
악의적 인 경고 목록이 생성되었습니다. 나는 그들을 통과해야 할 것이다. – Structure
실제 코드에는이 주석이 많지 않기를 바라고 있습니다. –