With respect to the following code segment,
foreach my $Index1 (sort { $A <=> $B } keys %{$dat1->{dat1}->[$Index2]->{Vector}})
{
if($dat1->{dat1}->[$Index2]->{Vector}->{$Index1} == 2.0) { next }
printf $sth
}
How to understand the my $Index1 (sort { $A <=> $B } keys %{$dat1->{dat1}->[$Index2]->{Vector}}) and ($dat1->{dat1}->[$Index2]->{Vector}->{$Index1} == 2.0)
How to understand their underlying logic piece by piece? Thanks.
It helps if you abstract the nested data structure away:
$dat1->{dat1}->[$Index2]->{Vector}is a way to access some piece of data (a hashref) deep in a nested data structure. It takes a hashref$dat1, accesses the data structure that is pointed to by a hash key"dat1"– and that key points to an array reference. You take the value from that array with an index$Index2– and that value is a reference to the next data structure (hashref). You take the value of that hash for the key"Vector"– which is another hashref.Now, let’s alias that Vector hashref with
$VectorHashRefvariable (I’m using the word alias loosely).Now, you are iterating over the keys of that hashref, sorted numerically (see Dan’s answer for details on how that works), and for each key, comparing the hash value to
2.0, printing something only if the value is NOT 2.0.To understand this well, you need to read somee data structures in Perl tutorial – for example Data Structures Cookbook (aka
perldoc perldsc)In short:
When you see something that looks likie
EXPRESSION->{KEY_EXPRESSION}, that means accessing the value (for a key specified by KEY_EXPRESSION) in a hashref, with the hash reference being the result ofEXPRESSION.When you see something that looks likie
EXPRESSION->[INDEX_EXPRESSION], that means accessing the value (for a array subscript specified by INDEX_EXPRESSION) in an array reference, with the array reference being the result ofEXPRESSION.