Dies ist das erste Mal, dass ich Hashes und Arrays auf diese Weise manipuliert habe - und es funktioniert. Grundsätzlich gibt es für jeden Schlüssel mehrere Werte, die ich aufzeichnen und dann in der Form "Schlüssel -> Wert -> Wert -> Wert ..." ausdrucken möchte.Ist dies der richtige Weg, um einen Perl-Hash zu erstellen, der Arrays verwendet?
Mein Code ist wie folgt. Ich bin überrascht, dass es funktioniert, so besorgt, dass es "aus Versehen" funktioniert. Ist dies der richtige Weg, um diese Aufgabe zu erfüllen, oder gibt es eine effizientere oder geeignete Methode?
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.
}
Sie sollten wirklich in Gebrauch streng sein; Verwenden Sie Warnungen; 'Modus – Zaid
Autsch, dass eine böse Liste der Warnungen produziert. Ich werde sie durchgehen müssen. – Structure
Ich hoffe, dass der echte Code nicht so viele Kommentare hat. –