Using hashes in a chaining manner confuses me a lot. For instance, I read the following Perl codes, how to understand them piece by piece?
$model->{result}->{forcast}->[$index]->{label} = 1;
$Neg{$examples->{result}->[$index]->{title}} = 1
In addition, why some items has $ , like $index; while others do not have, like label.
$index is wrapped in [ ] while others are wrapped in { }, what are the differences here?
Is $Neg{$examples->{result}->[$index]->{title}} = 1 equivalent to $Neg{$examples->{result}->[$index]->{title}} = 1
Consider:
->[]is used to dereference an array reference.->{}is used to dereference a hash reference.Let us scan it from the left:
$modelis a hash reference (due to it being used in the context:$model->{})resultis a hash key (as it does not have a$sigil prepended)$model->{result}is again a hash reference$model->{result}->{forcast}is an array reference (due to it being used in the context:$model->{result}->{forcast}->[])$indexis a variable set by the user that possibly contains the index of an array item$model->{result}->{forcast}->[$index]is a hash referencelabelis a hash key$model->{result}->{forcast}->[$index]->{label}sets1as the value for the hash keyHash keys can be barewords, which will be automatically quoted. So, specifying the hash key as
resultor'result'are the same.perldoc perldscis the cookbook for data structures.Data::Dumperis very helpful in viewing such data structures.